Linux Commands

Show Line Numbers in Vim

When you work with long files while using an editor, it is quite possible that you may lose track. In another case, running a program could also cause an error in any line that would be difficult to resolve without knowing the line numbers.

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

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:

vim

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:

:set number

Or:

:set nu

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:

:set relativenumber

Or:

:set rnu

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:

vim ~/.vimrc

Step 2: Enable the insert mode by pressing the “i” button and typing the following in the editor:

set number

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:

:set nonumber
:set nonu

2. You can also disable the relative number mode by using any of the following syntax:

:set norelativenumber
:set nornu

3. To turn off line number permanently whenever Vim starts, open the ./etc/vimrc file using the mentioned command:

vim ~/.vimrc

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:

:set nonumber

Or:

:set nonu

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.

About the author

Syeda Wardah Batool

I am a Software Engineer Graduate and Self Motivated Linux writer. I also love to read latest Linux books. Moreover, in my free time, i love to read books on Personal development.