How To Show Line Numbers in Vim/Vi?

Vim and vi are popular text editors that are used by developers, system administrators, geeks, network administrators. Even there are GUI implementations of the Vim/vi their are mostly used from the command line as a practical command line text editors. By default, Vim/vi do not show line numbers on the left side of the terminal. As an advanced command-line text editor Vim/Vi provides advanced features for showing line numbers like relative and hybrid line numbering.

Why Show Vim/Vi Line Numbers?

Line numbers can be used for different reasons from different points of user views. Below you can find some of them.

  • Later find a specific line or part of the file easily
  • Provide line number or line number range others
  • Explain specified part of the content in a document

Line Number Types

Vim/Vi provides 3 different line number types or modes. The default line number type is called as Absolute line numbers.

  • Absolute Line Numbers are used normal line numbers where line number 1 starts from the start of the given file and the start of the file is the reference point for counting line numbers.
  • Relative Line Numbers are used to show the distance to the cursor which is relative and change with the cursor movement.
  • Hybrid Line Numbers provides both Absolute and Relative Line numbers at the same time for practical usage.

Show Absolute Line Numbers

The most popular line numbering mode is absolute line numbers or simply line numbers. We can display line numbers by using the set number Vim/Vi command but first we should enter to the command mode by following these steps.

  1. First, enter the command mode by pressing the Esc key from the keyboard.
  2. Then press double-colon or : in order to start Vim/Vi command mode.
  3. The last step is using the following command to show or display absolute line numbers.
:set number

We can also use the command completion features where we can put set nu to show line numbers.

In general from the command mode the following : set number command can be used to show line numbers.

:set number

Make Absolute Line Numbers Permanent

As we quit from Vim/Vi the relative number configuration will be lost because the settings done via Vim/Vi command line are temporary. We can make the displaying absolute line number permanent by adding the following configuration to the settings file. For the current user, the configuration file is ~/.vimrc .If you want to make the relative line numbers permanent for all system users change the general configuration file which is used by all users. Add previously defined configuration into the file /etc/vim/vimrc .

:set number

Show Relative Line Numbers

Relative line numbers can be used to show line number according to the cursot position or line number. The relative line number command is set relativenumber by following steps below.

  1. First, enter the command mode by pressing the Esc key from the keyboard.
  2. Then press double-colon or : in order to start Vim/Vi command mode.
  3. The last step is using the following command to show or display absolute line numbers.
:set relativenumber

We can see that the current absolute line number of the cursor shown in the first row. The second row shows the relative line numbers according to the cursor.

Alternatively, we can use the shortened command for relative line number command set rnu.

:set rnu

Make Relative Line Numbers Permanent

As we quit from Vim/Vi the relative number configuration will be lost because the settings done via Vim/Vi command line are temporary. We can make the displaying relative line number permanent by adding the following configuration to the settings file. For the current user, the configuration file is ~/.vimrc .

:set relativenumber

If you want to make the relative line numbers permanent for all system users change the general configuration file which is used by all users. Add previously defined configuration into the file /etc/vim/vimrc .

Show Hybrid Line Numbers

As stated previously hybrid line numbers will show both absolute and relative line numbers at the same time. Actually, after the Vim 7.4, both relative and absolute lines numbers can be displayed at the same time. Also, we have seen previously that we can first show the absolute line number and then relative line numbers where both of them can be displayed without any conflict. For hybrid line numbers we will set both number and relativenumber configuration like below.

:set relativenumber number

Alternatively, we can enable the hybrid line numbers in separate commands like below which are the same as the previous example.

:set relativenumber
:set number

Change Line Number Column Width

For smaller files the lines numbers can be easily fit into the number column width but if the file is big and there are a lot of lines. We can use the configuration named numberwidth in order to set the line number column width. In the following example, we will set the line number width to 4. But keep in mind that this configuration will work for Vim 7.0 and later versions.

:set numberwidth=4

Leave a Comment