How to Show Line Numbers in Vim / Vi?

The vim editor is the improved version of the Vi text editor, which can be installed on Linux distributions using the default package. The vim text editor is used by different commands and provides all the advanced features of the text editor.

In this blog, we will illustrate how we can show the line numbers in the Vim text editor and explain the following:

Let’s get into vim installation first.

How to Install Vim Text Editor on Linux?

The vim installation package comes in the default repository of almost all Linux distributions. Moreover, the Vim text editor can be installed on Debian-based Linux distributions using the command:

$ sudo apt install vim -y

It can be installed in RHEL/CentOS-based Linux distributions using the command:

$ sudo yum install vim

Also, it can be installed on the Fedora-based Linux distributions using the command:

$ sudo dnf install vim

When the Vim text editor is installed on Linux, Now, let’s explore the methods to show line number in vim/vi.

Method 1: How to Show the Absolute Line Numbers in Vim/Vi?

We can use the command “:set number” to show the file line numbers by using the text editor’s command mode; for example, we will open the myfile.txt using the vim text editor:

$ vim myfile.txt

The file has been opened:

Press the “ESC” button on the keyboard to initialize the command mode and type “:set number” and press the “ENTER” key of the keyboard:

When the “ENTER” key is pressed, the line numbers will be assigned to all the lines of the file:

The number assigned using the “:set number” is known as absolute number and can be removed by using the command “:set nonumber”:

The numbers will be removed after the execution of the above command:

Method 2: How to Show the Relative Line Numbers in Vim/ Vi?

Another way to show the numbers with the lines is by using the relative numbers and which can be set by using the command “:set relativenumber”:

The relative numbers have been assigned:

Now, clear the relative numbers using the command “:set norelativenumber”:

Method 3: How to Show the Hybrid Line Numbers in Vim/ Vi?

The last method is of assigning the hybrid numbers, which will assign absolute as well as relative numbers by using the command “:set number relativenumber”:

The hybrid numbers have been assigned, which can be removed by using the command “:set nonumber norelativenumber”:

The numbers have been removed.

That;’s how we can use vim to get the line numbers.

Conclusion

To show the line numbers in Vim / Vi in Linux, use the command “:set number”, “:set relativenumber”, and “:set number relativenumber”. To use vim, ensure that it is installed on your system. This post has briefly explained the methods of showing the line numbers in a vim text editor.