Vim (Vi iMproved) is a text editor for Unix. It is free and open-source software. It is a popular text editor for system administration and development work. When you start Vim, it looks minimal but it is very powerful under the hood.

Depending on your level of comfort with Linux, you may use Vim for editing text files. You can use it to edit Bash scripts, configuration files, any programming language, and any other text files.

If you do decide to edit your program with Vim, it will be useful to show the vim line numbers next to the line you are editing. This will give it an easier path for you to debug your program. How do you display the line numbers when you are inside Vim? By default, the line numbers are not shown but you can change it to be shown by default.

In your home directory, there is a file called ~/.vimrc which contains the Vim configuration. You may not have this file if you haven’t made any customization to Vim. If this is the first time editing this file, run:

vim ~/.vimrc

This command will open the Vim configuration file. If it is empty, add the entry below to the file.

Absolute Line Numbers

To set the absolute vim line numbers, add the following line to the Vim configuration file below.

set number

Press the Esc key and press 😡 to save and exit the file. After making this change, if you run the previous Vim command again, you will see the line number on the left side of the terminal.

To verify the line number is what you expected, open a text file with Vim. Assume that you want to go to the tenth line in the file. To do so, press the Esc key and press :10 and then press the Enter key. On the left side column, you should see the number 10.

To turn off the line number for this file only, you can press the Esc key and type:

:set nonumber

After you press the Enter key, the line number will display. Since you have set to use line numbers in the Vim configuration file, this change is temporary. To make it permanent, remove this line from the configuration file.

Relative Line Numbers

Another option is to set relative vim line numbers. It is helpful to show the relative number to the cursor’s current position. The current line is marked 0. The line above and below the cursor is marked 1. The second line line above and below the cursor is marked 2.

To turn on the relative line numbers, add the line below to the Vim configuration file:

set relativenumber

You can also turn off the relative line numbers if it is turned on by press the Esc key, typing the command below, and pressing the Enter key.

:set norelativenumber

Hybrid Line Numbers

What if you want to enable the absolute vim line numbers and the relative vim line numbers at the same time? You can do so by turning on the hybrid line numbers.

This is done by turning on both the absolute line numbers and the relative line numbers in the Vim configuration file.

To turn on hybrid line numbers, add both lines below to the Vim configuration file:

set number
set relativenumber

As you can see, it is a combination of both the absolute and relative line numbers. You can also combine both lines into one line by adding:

set number relativenumber

Both commands operate the same one. Either one works. It is your preference which one you choose.

Leave a Comment