How to Show Line Numbers in Vim or Vi Text Editor

How to Show Line Numbers in Vim or Vi Text Editor
How to Show Line Numbers in Vim or Vi Text Editor

Show Line Numbers in Vim or Vi Text Editor

Vim or Vi is the popular text editor among the Linux System Administrators and Software Developers. Vim comes preinstalled on major Linux distributions and macOS. The Vim text editor is used to edit configuration files or program files. The Line Numbers are useful in code review, debugging, referring specific lines of code. The Line Numbers in Vim are not enabled by default. In this tutorial, you are going to learn how to Show Line Numbers in Vim or Vi Text Editor.

Vim supports the following three types of line numbers:

  1. Absolute Line umbers
  2. Hybrid Line Numbers
  3. Relative Line Numbers

1. Absolute Line Numbers in Vim

The Absolute Line Numbers are the normal line numbers. Absolute Line Numbers show the specific line number at the start of each line. To enable Absolute Line Numbers to follow below instructions:

First press Esc key to switch to normal mode:

Now press : and then type set number like given below:

:set number

The Absolute Line Numbers will be displayed as given below:

"How

Now you can disable Absolute Line Numbers by using the following command:

:set number!

"How

or You can also use the following command:

:set nonumber

2. Relative Line Numbers in Vim

The Relative Line Number system is very much handy when using the Vim text editor. Operations like deleting lines are done with respect to relative line numbers. In Relative Line Numbers, the number for current line is set to 0 then the lines below and above it numbered as 1, 2, 3…

You can enable Relative Line Numbers by following below instructions:

First press Esc key to switch to normal mode:

Now press the key : and then type set relativenumber like given below:

:set relativenumber

The Relative Line Numbers will be displayed as given below:

How to Show Line Numbers in Vim or Vi Text Editor - Relative Line Numbers
How to Show Line Numbers in Vim or Vi Text Editor – Relative Line Numbers

you can disable relative line numbers by using the following command:

:set relativenumber!

or You can also use a command like :set nornu, :set norelativenumber.

3. Hybrid Line Numbers in Vim

When you enable both Absolute Line Numbers and Relative Line Numbers is called Hybrid Line Numbers.

Hybrid Line numbers are as same as Relative Line Numbers but the difference is the Current Line Number will be numbered as Absolute Line Number instead of 0.

You can enable Relative Line Numbers by following below instructions:

First, enable Absolute Line Numbers by typing following command:

:set number

Then enable Relative Line Numbers by typing following command:

:set relativenumber

The Hybrid Line Numbers will look like given below:

How to Show Line Numbers in Vim or Vi Text Editor - Hybrid Line Numbers
How to Show Line Numbers in Vim or Vi Text Editor – Hybrid Line Numbers

To disable Hybrid Line Numbers type following commands one by one:

:set number!
:set relativenumber!

Permanently Enable Line Numbers

To permanently enable line numbers you should edit .vimrc configuration file. Enter the following command to open .vimrc file:

vim ~/vimrc

Now to permanently enable Absolute Line Number, add the following text to the file and save the file:

:set number

After making above changes save and exit the file.

Open File with Specific Line Number

To open file with specific file number following command syntax is used:

vim +LINENUMBER filename

For example, to open hello_world.c with line number 13 followed by + sign:

vim +13 hello_world.c
How to Show Line Numbers in Vim or Vi Text Editor - Specific Line Number
How to Show Line Numbers in Vim or Vi Text Editor – Specific Line Number

Conclusion

In this tutorial, you have learned How to Show Line Numbers in Vim or Vi Text Editor. If You have any queries regarding this please comment below.

LEAVE A REPLY

Please enter your comment!
Please enter your name here