vim

Toggling Line Numbers On/Off in the vi Text Editor

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:


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:


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:


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:


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:


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.

Best Gamepad Mapping Apps for Linux
If you like to play games on Linux with a gamepad instead of a typical keyboard and mouse input system, there are some useful apps for you. Many PC ga...
Instrumente utile pentru jucătorii Linux
Dacă vă place să jucați jocuri pe Linux, este posibil să fi folosit aplicații și utilitare precum Wine, Lutris și OBS Studio pentru a îmbunătăți exper...
Jocuri HD remasterizate pentru Linux care nu au avut niciodată lansare Linux mai devreme
Mulți dezvoltatori și editori de jocuri vin cu remasterizarea HD a jocurilor vechi pentru a prelungi durata de viață a francizei, vă rog fanilor să so...