On using relative line numbers
Working with relative line numbers (as opposed to absolute line numbers) is a popular technique commonly used in command-line modal editors. The most common operations performed using relative line numbers are:
Jumping to the relative line (e.g., moving the cursor 5 lines above or below in the editor).
Making a selection (e.g., select the 5 lines above or below the cursor).
Visual Studio Code supports working with line numbers only in a limited way, but it can be extended to support these operations.
The first step is to display the relative line numbers in the editor, which provides context for selecting the lines later on. This can be achieved by setting Editor: Line Numbers to relative in VSCode Settings for Text Editor (you can just open settings and type “line numbers”):
To have the same operations for jumping and selecting we will need to install an extension. I am using relative goto that provides exactly these two operations.
After installation I recommend to check or edit the keyboard shortcuts for these new operations. Alt + G
(jump) and Alt + S
(selection) should be the default:
Once done, these operations open a dialog where you can enter the desired relative line number, similar to Go to Line command for absolute numbers. Use minus (-) to select previous lines.
And that’s it! I use relative goto occasionally, but I’d prefer if there was native support instead. Do you use relative line numbers? Do you use any other extension? Let me know.
— Petr