Signup/Sign In

How To Show Line Numbers In Vim / Vi

Vim does not show line numbers by default. However, the numbers that indicate the beginning of each line may be beneficial to change the text using the Vim command. Many of these commands need you to supply line numbers.

In this lesson, learn how to display or conceal line numbers in Vim / VI in Linux.

3 Ways To Show Line Numbers In Vim

There are three distinct methods to show line numbers in Vim / Vi. You may configure the text editor to show:

Absolute line numbers: Represents the well-known chronological numbering.

Relative line numbers: Each line number is determined by its distance from the point of the cursor.

Hybrid line numbers: A mixture of the two above-mentioned alternatives.

Display Absolute Line Numbers

1. First, go to command mode by hitting Esc.

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

3. Finally, to display line numbers, type:

set number

Alternatively, you may use the shortened command:

set nu

Hide Absolute Line Numbers

1. Press Esc to change to command mode.

2. Use : to start the command bar.

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

set nonumber

If you like, you may also use its shortened form:

set nu!

Display Relative Line Numbers

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

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

3. Set Vim to show relative line numbers using the command:

set relativenumber

Or, use its shorter form:

set rnu

When you show relative numbers in Vim, each line is numbered by its distance from the cursor.

Take a look at the location of the pointer in the picture above. This position is denoted as 0. Accordingly, lines above and beneath the cursor are classified as 1, the next ones 2 and so on.

Turn off Relative Line Numbers

1. Switch to regular mode using Esc.

2. Press :.

3. Now you may switch off relative line numbers by entering 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

Hybrid line numbering is a blend of absolute and relative numbering. It employs relative line numbering for everything except the cursor line, which is denoted by its absolute number instead of zero.

Turn off Hybrid Line Numbers

1. Move to command mode(Esc).

2. Start by typing :.

3. Then, disable hybrid line numbering using the command:

set nonumber norelativenumber

Permanently Enable Vim Line Numbers

Vim doesn’t contain line numbering by default. Activating line numbering only applies to the file you are working on. Once the file is closed, Vim returns to concealing line numbers.

1. To activate line numbers permanently, open the Vim configuration file with the name .vimrc.

vim ~/.vimrc

2. Add the following line to the file:

:set [prefered numbering mode]

Conclusion

In this article, you learned how to show or conceal line numbers in the Linux Vim text editor.