Sharing a link to line(s) of code
One common problem in a team setting is the ability to point other developers to a specific piece of code. Often we have the code open in front of us and we want them to open it at the same location as well.
Fortunately the major online code repository platforms like GitHub and GitLab can accept and generate links to a particular line of code. We can typically do so by clicking on the line number and use the context menu to get the link:
One lesser known thing is the ability to select multiple lines as well. By clicking again on another line number while holding Shift
, we can select a range of lines and use the same “Copy permalink” action to get the link.
And while the feature is certainly useful, more often than not we are not browsing the code online like that. So we need a way to get the link from our code editor. That’s where VSCode extensions like Open in GitHub come in handy.
With the extension installed, we can select a range of lines directly in the editor locally and get the link by using the “Copy file permalink“ action from the command palette (F1
to open):
Now we can send a link to the selection to anyone and they will be directed to GitHub, with the lines highlighted:
Perfect!
The only missing thing is that the extension doesn’t copy a link for unselected lines by default. We need to fix that first:
Open VSCode settings (
Ctrl + ,
)Search for the extension name
@ext:fabiospampinato.vscode-open-in-github
Turn on “Use local line”
After the change the “Copy File Permalink” action will work without the need to select the lines of code first. It will generate a link to the current line where the cursor is placed.
Let me know how you share your links,
Petr