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.
3 Options to Show Line Numbers 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 Enable Vim Line Numbers
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:
Note: If you do not have a .vimrc file in your home directory, create one with
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.