How to Show Line Numbers in Vim or Vi Text Editor

Table of Contents
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:
- Absolute Line umbers
- Hybrid Line Numbers
- 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:
Now you can disable Absolute Line Numbers by using the following command:
:set number!
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:

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:

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

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.
LATEST POSTS
-
How to Set Up Cron Job on CentOS 8
-
How to Install and Use Docker on Ubuntu 18.04
-
How to Install PHP on Ubuntu 18.04
-
How to Kill Process in Linux
-
How to Install Python on Linux Mint 19
-
How To Secure Nginx with Let’s Encrypt SSL on Debian 9
-
How to install LEMP Stack on Ubuntu 18.04
-
How to Create Sudo User on CentOS
-
How to Install Gradle on Debian 10
-
How to Install Apache on Linux Mint 19
-
How to Install Opera Browser on Debian 9
-
How to Install Docker Compose on Debian 9