vim

Toggling Line Numbers On/Off in the vi Text Editor

The line numbers shown in a text editor can greatly enhance a programmer’s experience writing and reading code. There are several text editors available for the Linux operating system, including the popular and powerful vi text editor, and these editors can be used to create and modify various file types.

The vi editor provides three different types of line numbers: absolute, relative, and a hybrid combining features of absolute and relative. In this article, we will discuss method that can be used to change the line number type shown in the vi text editor.

Note: Linux Mint 20 is used to demonstrate all the methods discussed below.

Showing Absolute Line Numbers

Absolute (or regular) line numbers are the line numbers preferred by most users. To display the absolute line numbers in vi, take the following steps:

Launch the terminal.


Open any file that can be edited with vi text editor by running the command `$ sudo vi [file name]` in the terminal, as follows:


Press the Esc key to enter Command Mode .

This step is important because, while the text editor has several modes of operation, for our present goal, we have to be in Command Mode.

Run the command `:set number`, as follows.


When you hit Enter to execute this command, the absolute line numbers will appear at the beginning of each line of your file, as shown below:

Hiding Absolute Line Numbers

You can hide the absolute line numbers in vi by taking the following steps:

  • Once you have a file opened in vi, enter Command Mode by pressing the Esc key
  • Execute the command `:set nonumber`.


After executing this command, the absolute line numbers will no longer be shown, as shown in the image below:

Showing Relative Line Numbers

For relative line numbers, the line in the file to which your cursor is currently pointing is assigned the number zero, and all other line numbers are calculated relative to that line.

You can show relative line numbers in vi by taking the following the steps:

  • Once you have a file opened in vi, press the Esc key to enter Command Mode.
  • Execute the following command `:set relativenumber`.


When you execute this command, relative line numbers will appear at the beginning of every line of your file. In our case, because the cursor was pointing to the third line of the file, the third line is assigned a value of 0, and the other line numbers are calculated accordingly, as shown below:

Hiding Relative Line Numbers

To hide the relative line numbers, you can take the following steps:

  • Once you have a file opened in vi, press the Esc key to enter Command Mode
  • Execute the command `:set norelativenumber`.


When you execute this command, the relative line numbers have be removed, as shown in the image below:

Showing Hybrid Line Numbers

For hybrid line numbers, the line to which your cursor is pointing is assigned its actual absolute line number, but all other line numbers are relative values.

We can show hybrid line numbers by taking the following the steps:

  • Once you have a file opened in vi, press the Esc key to enter Command Mode
  • Execute the command `:set number relativenumber`.


When this command is executed, hybrid line numbers will be shown at the beginning of every line of the file. In our case, because the cursor was pointing to the third line of the file, the number 3 was assigned to the third line, and all other line numbers were calculated accordingly, as shown below:

Hiding Hybrid Line Numbers

To hide hybrid line numbers, you can take the following steps:

  • Once you have a file opened in vi, press the Esc key to enter Command Mode
  • Execute the command `:set nonumber norelativenumber`.


When you execute this command, the hybrid line numbers will be removed, as shown in the image below:

Conclusion

The commands discussed in this article can be easily used to toggle absolute, relative, and hybrid line numbers on/off in the vi text editor.

About the author

Aqsa Yasin

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.