Vim does not display line numbers by default. However, the numbers that mark the beginning of each line may be useful to modify text with the Vim command. Many of these commands require you to specify line numbers.
In this tutorial, learn how to show or hide line numbers in Vim/VI in Linux.
Show Line Numbering in Vim
There are three (3) different ways to display line numbers in Vim / Vi. You can set the text editor to show:
- Absolute line numbers: Represents the well-known chronological numbering.
- Relative line numbers: Each line number is defined by its distance from the position of the cursor.
- Hybrid line numbers: A combination of the two above-mentioned options.
Display Absolute Line Numbers
1. First, switch to command mode by pressing
2. Then, press
: to prompt a command line at the bottom of the file.
3. Finally, to show line numbers, type:
Alternatively, you can use the abbreviated command:
Hide Absolute Line Numbers
Esc to move to command mode.
: to start the command bar.
3. Hide line numbers by typing in the following command:
If you prefer, you can also use its abbreviated form:
Display Relative Line Numbers
1. Ensure you are in command mode by pressing
2. Next, press
: and prompt a command line.
3. Set Vim to display relative line numbers with the command:
Or, use its shortened form:
When you display relative numbers in Vim, each line is numbered by its distance from the cursor.
Take a look at the position of the cursor in the image above. This position is marked as 0. Accordingly, lines above and under the cursor are labeled as 1, the next ones 2 and so on.
Turn off Relative Line Numbers
1. Switch to normal mode with
3. Now you can turn off relative line numbers by typing one of the following commands:
set nonumber norelativenumber
set nonu nornu
Display Hybrid Line Numbers
Esc to make sure you are in command mode.
2. Open the command line in Vim by pressing
3. Activate hybrid line numbering by typing:
set number relativenumber
Hybrid line numbering is a combination of absolute and relative numbering. It uses relative line numbering for everything except the cursor line, which is marked by its absolute number instead of zero.
Turn off Hybrid Line Numbers
1. Move to command mode (
2. Start by typing
3. Then, deactivate hybrid line numbering with the command:
set nonumber norelativenumber
Permanently Show Line Numbers in Vim
Since Vim doesn’t include line numbering by default, activating line numbering only applies to the file you are working in. Once the file is closed, Vim returns to hiding line numbers.
1. To enable line numbers permanently, open the Vim configuration file under the name
2. Add the following line to the file:
3. Save and exit the file by pressing Esc to switch to command mode and type:
By default, this enables absolute line numbering.
Note: If you do not have a .vimrc file in your home directory, create one with
Additional Settings for Showing Line Numbers in Vim
Read on to learn some additional options Vim offers for showing line numbering.
Change Column Width for Numbering
Vim versions 7 and newer include the option of modifying the width of the gutter column used for numbering.
To change the column width, switch to command mode with Esc and type:
To set the gutter column width permanently, use the same syntax and add the line to the .vimrc file.
For instance, to expand the width to have the value 10, you would use:
Change Text Wrapping for the Number Column
To configure text wrapping for the number column, press Esc and run:
Alternatively, add the options to the ~/.vimrc file:
Change Line Number Color
To change the color of the numbers displaying line numbering, press Esc and run:
highlight LineNr term=bold cterm=NONE ctermfg=[color] ctermbg=NONE gui=NONE guifg=[color] guibg=NONE
For example, to change the line numbering to red, you would type:
highlight LineNr term=bold cterm=NONE ctermfg=Red ctermbg=NONE gui=NONE guifg=Red guibg=NONE
In this tutorial, you learned how to display or hide line numbers in the Linux Vim text editor.
Vim allows many user-friendly modifications; adding line numbers is just one of them. To go a step further, find out how to use vim color schemes.