How to Show Line Numbers in Vim and Vi

When editing code, configuration files, or scripts in Vim, line numbers make it easier to jump to a specific line, review errors, and discuss changes with another person.
Vim does not show line numbers by default. You can enable them for the current session with :set number, or make them permanent by adding set number to your .vimrc file.
This guide explains how to show, hide, toggle, and save line number settings in Vim and Vi.
Quick Reference
For a printable quick reference, see the Vim cheatsheet .
| Task | Command |
|---|---|
| Show absolute line numbers | :set number |
| Hide absolute line numbers | :set nonumber |
| Toggle absolute line numbers | :set number! |
| Show relative line numbers | :set relativenumber |
| Hide relative line numbers | :set norelativenumber |
| Show hybrid line numbers | :set number relativenumber |
| Check current absolute setting | :set number? |
| Make absolute line numbers permanent | Add set number to ~/.vimrc |
Absolute Line Numbers
Absolute line numbering displays the actual line number next to each line of text.
To activate the line numbering, set the number flag:
Press the
Esckey to switch to normal mode.Press
:(colon) and the cursor will move at the bottom left corner of the screen. Typeset numberorset nuand hitEnter.vimrc:set number
Line numbers will be displayed at the left side of the screen:

To disable absolute line numbers, run the :set nonumber or set nonu commands:
:set nonumberYou can also toggle the line numbers with :set number! or :set nu!:
:set number!Relative Line Numbers
When the relative line numbering is enabled, the current line is shown as 0. The lines above and below from the current line are incrementally numbered (1, 2, 3, etc.).
Relative line numbers are a Vim and Neovim feature and are not available in classic vi.
Relative line mode is handy because many Vim operations, such as moving up/down and deleting lines work on relative line numbers.
For example, to delete the next ten lines below the cursor, you would use the d10j command. With relative line numbers enabled, you have a better visual overview of the code.
To enable the relative line numbering, switch to the command mode and enter :set relativenumber or :set rnu:
:set relativenumber
To disable the relative line numbering, type :set norelativenumber or set nornu:
:set norelativenumberTo toggle the relative line numbering, use the :set relativenumber! or :set rnu! command:
:set relativenumber!You can check the current setting with:
:set relativenumber?Hybrid Line Numbers
In Vim 7.4 and later, enabling both the absolute and relative line numbers at the same time sets up the hybrid line number mode.
Hybrid line numbering is the same as the relative line numbering with the only difference being that the current line instead of showing 0 shows its absolute line number.
To turn on the hybrid line numbering, run both the number and relativenumber commands:
:set number relativenumberThe short version is:
:set nu rnu
The same can be achieved by running the commands one by one:
:set number
:set relativenumberTo disable hybrid mode, turn off both the absolute and relative numbering.
Make Line Numbers Permanent
Commands such as :set number apply only to the current Vim session. To show line numbers every time you open Vim, add the setting to your .vimrc file.
Open the Vim configuration file:
vim ~/.vimrcAdd this line to enable absolute line numbers by default:
set numberIf you prefer relative line numbers, add:
set relativenumberFor hybrid numbering, where the current line shows its absolute number and nearby lines show relative numbers, add both settings:
set number
set relativenumberTo toggle line numbers with a key, add a mapping like this:
nnoremap <F2> :set number!<CR>Conclusion
Use :set number when you need absolute line numbers for the current Vim session. Add set number to ~/.vimrc when you want Vim to show line numbers every time it starts.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors

Dejan Panovski
Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.
View author page