vim show line numbers

Vim is a powerful and highly configurable text editor used in CLI (command line interface) for creating and changing any kind of text.

Vim doesn’t show line numbers by default, but you can easily enable it in your configuration.

When you are working on a bash script and debugging an issue, then you can debug your issue easily by pin-pointing the line number.

Vim has three modes to help navigate around files:

  1. Absolute
  2. Relative
  3. Hybrid.

In this tutorial, we will show you how to show or hide line number in Vim text editor.

Install Vim

By default, Vim is installed in most Linux-based operating systems.

If not installed you can install it by running the following command:

apt-get install vim -y

You can display all the options available in Vim with the following command:

vim --help

You should see the following screen:

Enable Absolute Line Numbers

With absolute line numbers, you can show the line number for each line in the file you’re working on.

First, you will need to start the Vim text editor.

Open your terminal and run the following command:

vim /etc/passwd

You should see the following screen:

Next, press the ESC key to switch to command mode and type

: set number

and press Enter to enable the line numbering as shown below:

To disable the line numbering, type

: set nonumber

and press Enter as shown below:

Enable Relative Line Numbers

Relative line numbers displays line numbers relative to the current cursor position to show the distance to that line.

To enable the relative line numbers, press

ESC

and type

: set relativenumber

or

: set rnu

and then hit Enter button as shown below:

In the above screen, you should see that the current line is marked 0.

Now, move the cursor down, you should see the following screen:

You can disable the relative line numbers using

: set norelativenumber or : set nornu

The relative line numbers are very useful when working in lengthy files.

For example, if you want to jump to 20 lines up, you can press 20k to get there.

Enable Hybrid Line Numbers

Enabling absolute number and relative number at the same time produces hybrid line number mode.

To enable the hybrid line numbers, Press ESC, and type

: set number relativenumber

or

: set nu rnu

and then hit Enter button as shown below:

To disable the hybrid line numbers, Press ESC, and then type

: set nonumber norelativenumber

or

: set nonu nornu

and then hit the Enter button.

In short, the hybrid line numbers give you an idea of where you are in the file.

Open File at Specific Line Number

If you are working with big file and want to open the file at specific line number, you can use the following command:

vim +linenumber filename

For example, open the file /var/log/syslog at line number 100 using the following command:

vim +100 /var/log/syslog

You should see the following screen:

Configure Vim to Enable Line Numbering Permanently

If you want to enable line numbering every time you start Vim, you can do it by editing ~/.vimrc file:

nano ~/.vimrc

Add the following line:

:set number

Save and close the file when you are finished.

List All Vim Settings

You can also list all Vim settings using : set all command.

First, start the Vim editor, press ESC , type : set all and hit Enter button to list all Vim settings as shown below:

Conclusion

Thats wraps it up for now with Enabling Line Numbers in VIM. We hope you have now have enough knowledge on how to enable and disable different line numbers mode in Vim. Feel free to ask me if you have any questions below in the comment section!