Vi/vim text editor is popular for Linux users because of its many unique features. Developers and regular users use its many features, one of which is that Vi can enable/disable its line number. The line number is the essential feature of Vi text editors, primarily for programming.
Since the editor in Vim/vi has many commands that use the line number for navigation, this is the reason the line number is even more critical for Vim/Vi. Knowing just the number of a specific line, you can quickly move the cursor on it. Now, we will discuss this feature on how to enable/disable line numbering in Vi.
What Are Line Numbers in Vi/Vim?
The essential features of Vi/Vim are challenging to figure out, so it is known as a power-user-oriented text editor. It proves to be so tricky that even quitting it can be challenging for new users.
So, it’s important to note that Vi shows you the current line number at the bottom-right side of the window, even in its most basic form.
How to Enable/Disable Line Numbering in Vi
There are three different line numbers in Vi: absolute line numbering, relative line numbering, and hybrid line numbering. There are various methods by which we can enable/disable line numbers in multiple forms. We will discuss the different line numbers and ways to enable/disable them here.
Absolute Line Numbering
Absolute lines numbering, as per their names, represents the sequential numbering of lines. If you wish, we can number every row starting from the beginning and going to the end. Please make sure you are in command mode. Press Esc if you are not sure which mode you are in. Enter the following command:
Now, you can see the line number in your left-hand column, and you remain in command mode. Similarly, you can enable line numbers, and, in the same way, you can disable line numbers by repeating the process. For this, type the following command:
Note: You can also use abbreviated versions of every command here, set nonu and set nu, respectively.
Relative Line Numbering
Vim’s syntax for navigating commands works well with relative line numbers. The numbering method shows line numbers around the cursor’s position. This comes in handy when dealing with someone who needs to point you to specific lines relative to a particular line or view your code from a specific context. It allows you to move the cursor directly up the lines. Activate the relative numbers by entering the command mode by pressing Esc. Afterward, use the following command and press Enter button:
This setting will automatically show 0 along the current line. Also, Vi/Vim will prefix your bottom and top lines with 1. All other lines will remain in their original position. Activate both with this setting and the standard number setting simultaneously. However, your current line will still show 0 instead of the actual line number.
The relative line numbering commands must be followed by an exclamation point. Afterward, you can disable relative row numbering using these two commands:
Hybrid Line Numbering
Line numbering combines absolute and relative numbers. Starting with Vi/Vim 7.4, you can enable the hybrid line numbering. The relative and absolute line numbers can be viewed side by side with this method. Thus, making it easy to refer to any line in your file.
To enable it, start the command mode by pressing the Esc key and typing the following command to enable relative and absolute numbering:
They are also disabled in the same way. We have to disable both relative and absolute just like we usually do. To disable hybrid line numbering, type the following command and press Enter:
Permanently Enables Line Numbers in Vi/Vim
There is no permanent solution to any of these problems. Once Vi/Vim is turned off and back on, it returns to the default mode, and the line numbers are hidden.
Depending on the system, the location of the Vi/Vim settings file may vary. Mostly this file is found in your home directory named .vimrc. Any set command you place in this file will become effective by default.
Open the Vi/Vim configuration file .vimrc with the following command:
You can enable line numbering permanently by adding the following command to the .vimrc file.
If you do not already have a .vimrc file in your home directory, this command will help you create one.
The next time you open the editor, the line numbers will appear in */.vimrc by default. Using the set nonu process, you can hide them interactively.
This article explains how line numbers are enabled and disabled in Vi/Vim. There are mainly three types of line numbering in Vi text editor: absolute, relative, and hybrid. We made you aware of the methods to enable/disable these three types of line numbering in Vi. We explained the method to enable line numbers permanently. We hope you found this article helpful. If you have any queries related to this article, you can freely check the other Linux Hint articles.