Vim is a powerful and highly configurable command line editor that comes installed with most of the operating systems. It offers many useful features for editing and configuration of files. However, some of its useful features are disabled by default. One of them is line numbering. With Vim line numbering features, you can display line numbering at the beginning of each line which comes helpful when modifying the text. Line numbers are also useful in debugging scripts, code review and configuration files.
Vim has following three line numbering modes:
This post covers how to show or hide line numbers in Vim/Vi text editor. We will discuss all three modes.
Note:
It is the standard line numbering mode which shows the line numbers in the beginning of each line in the file.
To show absolute line numbering, follow the below steps:
1. Switch to Vim’s Normal operation mode by hitting the Esc key.
2. Then hit : and type the below command and hit Enter.
set number
or you can use the below abbreviation after hitting the “:” key:
set nu
Now you will see the absolute line numbers at the beginning of each line.
To hide absolute line numbering, follow the below steps:
1. Switch to Vim’s Normal operation mode by hitting the Esc key.
2. Then hit : and type the below command and hit Enter.
set nonumber
or you can use the below command after hitting the “:” key and hit Enter:
set number!
You can also use the abbreviated form of the above commands set nonu or set nu! after pressing the : to hide absolute line numbers.
In relative line numbering mode, the current line is marked as 0 while all the above and below lines are incrementally numbered (1,2,3,…) relative to the current line.
To show relative line numbering, follow the below steps:
1. Switch to Vim’s Normal operation mode by hitting the Esc key.
2. Then hit : and type the below command and hit Enter.
set relativenumber
or you can use the below abbreviation after hitting the “:” key:
set rnu
Now you will see the current line number marked as 0 and if you move the cursor up or down, you will see the numbers marked as 1,2 3,….
To hide relative line numbering, follow the below steps:
1. Switch to Vim’s Normal operation mode. Hit the Esc key to do so:
2. Then hit : and type the below command and hit Enter:
set norelativenumber
or you can use the below command after hitting the “:” key and hit Enter:
set relativenumber!
You can also use the shortened form of the above commands set nornu or set rnu!.
Hybrid line numbering enables both absolute and relative line numbering. It is similar to the relative line numbering except the current line shows its absolute number instead of showing 0.
To show relative line numbering, follow the below steps:
1. Switch to Vim’s Normal operation mode by hitting the Esc key.
2. Then hit : and type the below command and hit Enter.
set number relativenumber
or type the below commands one by one after hitting the “:” key for once:
set number
set relativenumber
To hide hybrid line numbering, follow the below steps:
1. Switch to Vim’s Normal operation mode by hitting the Esc key.
2. Then hit : and type the below command and hit Enter:
set nonumber norelativenumber
or type the below commands one by one after hitting the “:” key for once:
set number!
set relativenumber!
You can also use the abbreviated form of the above commands set nonu nornu or set nu! set rnu!(one by one) to hide absolute line numbers.
The method we have discussed above only enables the line numbering for the currently opened file. To enable line numbering for all the files that opens in Vim, follow the below procedure:
1. Edit .vimrc configuration file using the below command in Terminal:
$ vim ~/.vimrc
2. Then in the insert mode, use the following commands:
To enable absolute line numbering mode, the entry would be:
set number
To enable relative line numbering mode, the entry would be:
set relativenumber
To enable hybrid line numbering mode, the entry would be:
set number relativenumber
Then press Esc and type :wq to save and exit the file.
That is all there is to it! By following the procedures discussed in this article, you can show or hide line numbers in Vim/Vi text editor. You have learned about different line numbering modes and how to enable them for a current file or permanently for all files.
Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications including CCNA RS, SCP, and ACE. As an IT engineer and technical author, he writes for various websites.
2024 was the GenAI year. With new and more performant LLMs and a higher number…
Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…
Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…
At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…
I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…
Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…