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.

Middle mouse button not working in Windows 10
The middle mouse button helps you scroll through long webpages and screens with a lot of data. If that stops, well you will end up using the keyboard ...
How to change Left & Right mouse buttons on Windows 10 PC
It's quite a norm that all computer mouse devices are ergonomically designed for right-handed users. But there are mouse devices available which are s...
Emulate Mouse clicks by hovering using Clickless Mouse in Windows 10
Using a mouse or keyboard in the wrong posture of excessive usage can result in a lot of health issues, including strain, carpal tunnel syndrome, and ...