Line numbers should be displayed in an editor to avoid any hurdle specifically while debugging programs. This also increases the overall readability of files and help user to understand better. Vim, a powerful editing tool in Linux and Unix-like systems, is mainly used by system administrators, developers to write code, and Linux users to perform multiple functions.
- How to Show Line Numbers in Vim
- How to Display Relative Line Numbers in Vim
- How to Enable Line Number Permanently in Vim
- How to Disable Line Numbers in Vim
- Conclusion
How to Show Line Numbers in Vim
By default, the Vim editor doesn’t show the line numbers as this feature is disabled in the backend. But we can enable it easily even permanently when using Vim. Read the guide to find out the way:
We are performing this on the Ubuntu 22.04 version.
Step 1: Open the Vim Editor by typing Vim in the terminal:
As you can see, line numbers are not displayed with the lines.
Step 2: Press the ESC button from the keyboard to change the current (insert) to command mode; type :set number or :set nu in Vim:
Or:
Step 3: Press the Enter button to make changes:
How to Display Relative Line Numbers in Vim
If you want to show the relative line numbers; use the following steps in the Vim editor:
Step 1: Press Esc to switch to command mode and type :set relative number or :set rnu in the Vim’s screen:
Or:
Step 2: After typing the above syntax, hit the Enter button:
How to Enable Line Number Permanently in Vim
If you want to enable the line number by default whenever you start working with Vim; this can be done by updating the ~/.vimrc file.
Step 1: Open the ~/.vimrc file by using the mentioned command:
Step 2: Enable the insert mode by pressing the “i” button and typing the following in the editor:
Now, save the file by pressing :w and hit the Enter button
How to Disable Line Numbers in Vim
1. To turn off line the line numbers in Vim, open the Vim Editor, and type any of the following in it:
2. You can also disable the relative number mode by using any of the following syntax:
3. To turn off line number permanently whenever Vim starts, open the ./etc/vimrc file using the mentioned command:
Then type :set nonumber or :set nonu in the file; press ESC to change vim mode and type :w in it to save the file:
Or:
To exit the file type :qa in it and press the Enter button.
Conclusion
Vim is a powerful and user-friendly editing tool in Linux and Unix-based systems. While working with the Vim, line numbers are disabled by default. It is better to enable line numbers to avoid any difficulty in the document. Another advantage is you can easily edit the document when line numbers are mentioned. This tutorial has mentioned how to enable the line number in the Vim editor. We have also seen a way to permanently add the line number whenever the Vim starts. Moreover, the process of disabling it is also mentioned in the article.