Last updated: Apr 6, 2024
Reading time·3 min
Displaying line numbers as relative to the current cursor position is something you might be used to from using VIM.
You don't have to set up any external configurations or install any extensions to achieve this as the setting is natively supported.
To enable relative line numbers in Visual Studio Code:
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.You can also open the settings screen by pressing Ctrl
+ ,
on Windows and
Linux or Cmd
+ ,
on macOS.
relative
.If the setting is set to relative, the line numbers are displayed relative to the current cursor position.
Here is a short clip that demonstrates how this works.
The other possible options for the Editor: Line Numbers setting are:
off
- to not show line numbers at all.on
- to show absolute line numbers.interval
- to render line numbers every 10 seconds.By default, the setting is set to on
to show absolute line numbers.
Here is a clip that demonstrates what using absolute line numbers looks like.
An alternative approach to enable relative line numbers is to do it in your global settings.json file.
Ctrl
+ Shift
+ P
(or Command
+ Shift
+ P
on macOS).F1
to open the Command Palette.Type user settings json.
Click on Preferences: Open User Settings (JSON)
editor.lineNumbers
setting to relative
."editor.lineNumbers": "relative",
You can also set the line numbers setting to relative
for the specific
project, in a local settings.json
file:
In the root directory of your project, create a .vscode folder.
Create a settings.json
file in the .vscode
folder.
Add the following code to your settings.json
file.
{ "editor.lineNumbers": "relative" }
Once you set the setting to relative
, line numbers will be displayed relative
to the current cursor position for your current project.
Note that the settings you set in .vscode/settings.json
are only applied to
the current project (not globally).
If you want to apply the setting to the currently logged-in user, use the approach from the previous subheading.
You can learn more about the related topics by checking out the following tutorials: