Introduction

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.

Tutorial on how to display line numbers in Vim or vi text editor


Note: If you don't have Vim installed, read our tutorials on How to Install Vim on Ubuntu or How to Install Vim on CentOS.


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 Esc.

2. Then, press : to prompt a command line at the bottom of the file.

3. Finally, to show line numbers, type:

set number

Alternatively, you can use the abbreviated command:

set nu

Showing line number in Vim text editor in linux

Hide Absolute Line Numbers

1. Press Esc to move to command mode.

2. Use : to start the command bar.

3. Hide line numbers by typing in the following command:

set nonumber

If you prefer, you can also use its abbreviated form:

set nu!

Hide line number in Vim.

Display Relative Line Numbers

1. Ensure you are in command mode by pressing Esc.

2. Next, press : and prompt a command line.

3. Set Vim to display relative line numbers with the command:

set relativenumber

Or, use its shortened form:

set rnu

displaying an example of vscode vim relative line numbers

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 Esc.

2. Press :.

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

1. Use 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

Vim set hybrid line numbers command screenshot example

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 (Esc).

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 .vimrc.

vim ~/.vimrc

2. Add the following line to the file:

:set [prefered_numbering_mode]

Note: If you do not have a .vimrc file in your home directory, create one with vim .vimrc.


Conclusion

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.


Next you should also read