Relative Line Numbers in Visual Studio Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
3 min

banner

# Relative Line Numbers in Visual Studio Code

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:

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings and select Preferences: Open User Settings.

open user settings

You can also open the settings screen by pressing Ctrl + , on Windows and Linux or Cmd + , on macOS.

  1. Type editor line numbers and set the Editor: Line Numbers dropdown setting to relative.

set editor line numbers to relative

If the setting is set to relative, the line numbers are displayed relative to the current cursor position.

set editor line numbers to relative

Here is a short clip that demonstrates how this works.

show relative line numbers

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.

absolute line numbers

# Enabling relative line numbers in settings.json

An alternative approach to enable relative line numbers is to do it in your global settings.json file.

  1. Press Ctrl + Shift + P (or Command + Shift + P on macOS).
Note: you can also press F1 to open the Command Palette.
  1. Type user settings json.

  2. Click on Preferences: Open User Settings (JSON)

preferences open user settings

  1. Set the editor.lineNumbers setting to relative.
settings.json
"editor.lineNumbers": "relative",

set editor line numbers to relative

# Enable relative line numbers using a local settings.json file

You can also set the line numbers setting to relative for the specific project, in a local settings.json file:

  1. In the root directory of your project, create a .vscode folder.

  2. Create a settings.json file in the .vscode folder.

  3. Add the following code to your settings.json file.

.vscode/settings.json
{ "editor.lineNumbers": "relative" }

enable relative line numbers in local settings json

Once you set the setting to relative, line numbers will be displayed relative to the current cursor position for your current project.

show relative line numbers

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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.