Want to show line numbers in Vim?
Well, there are 3 types of line numbering in Vim:
- Absolute: Show line numbers starting from 1.
- Relative: Show line numbers from 0.
- Hybrid: Uses two columns and shows both absolute and relative numbers.
Here's how you can enable and disable them in Vim (make sure you are in normal mode before using them):
Type | Activate | Deactivate | Description |
---|---|---|---|
Absolute | :set nu | :set nonu | Show number starting from 1. |
Relative | :set rnu | :set nornu | Your current line will always be noted as the 0th line. |
Hybrid | :set nu rnu | :set nonu nornu | Shows the both relative and absolute numbers using two number columns. |
Wanna know how? Here's how you do it.
Enable absolute line numbering in Vim
As I mentioned earlier, absolute line numbers will start from 0 and if you want to have that behavior, follow 2 simple steps to enable it:
- Press
Esc
key to switch to normal mode - Press the colon key (
:
) and typeset nu
orset number
and hit theEnter
key
Confused? Here's how you do it:
Pretty easy. Right?
But what if you want to disable it?
Well, for that, all you have to do is press the colon key :
and type set nonu
or set nonumber
and it will disable the absolute number in Vim:
Make absolute line numbering permanent
If you liked the absolute line numbering, you can make it permanent by making a few changes to the vimrc
file. Let me show you how.
First, open the vimrc
file using the following:
vi ~/.vimrc
Now, press the i
key to get into the insert mode and type the following:
set number
Once done, press the colon key :
and type wq
to save and exit from the Vim editor.
From now on, if you open any file with the Vim, you will see absolute numbering by default.
Enable relative line numbering in Vim
When enabled, it will show the current line a 0th line which is quite helpful as you don't have to do mental math and you can go to a specific line pretty easily.
To enable relative line numbering, you have to follow two simple steps:
- Press the
Esc
key to switch to normal mode. - Press the colon key
:
and typeset rnu
orset relativenumber
and then hit theEnter
key.
Let me show you how you perform those steps in actual life:
For some reason, if you want to disable it, all you have to do is press the colon key :
and type set nornu
or set norelativenumber
and it will disable relative numbering:
Make relative line numbering permanent in Vim
If you want to use relative line numbering every time you use Vim, it is a good idea to make it permanent. For that, you have to make some changes in vimrc
file.
First, open the vimrc
file using the following command:
vi ~/.vimrc
Now, press the i
key to switch to insert mode and enter the following lines in the file:
set relativenumber
To save changes and exit from Vim, press the colon key :
and type wq
.
From now on, you will see relative numbering every time you use Vim.
Enable hybrid line numbering in Vim
Hybrid line numbering is a blend of absolute and relative numbering and in my opinion, it gives you the best of both worlds.
To enable hybrid line numbering, you need to follow two simple steps:
- Press
Esc
key to switch to the normal mode. - Press the colon key
:
and typeset nu rnu
orset number relativenumber
and then press theEnter
key.
Here's how you follow two steps:
For some reason, if you don't find it useful to your workflow, it can be disabled by simply pressing the colon key :
and typing set nonu nornu
or set nonumber norelativenumber
and press the Enter
key:
That was quick. Isn't it?
Make hybrid line numbering permanent in Vim
To make hybrid line numbering permanent in Vim, first, open the vimrc
file using the following:
vi ~/.vimrc
Now, press the i
button to switch to insert mode and enter the following:
set number relativenumber
To save changes and exit from Vim, press the colon key :
and type wq
, and hit Enter.
That's it!
New to Vim? Let me help
I know Vim overwhelms you with the plethora of keybindings and an entirely different workflow than the other text editors but once you get a grip on it, there's no going back.
I will recommend you start by learning what are the Vim modes:
Once done, learn some basic keybindings of Vim:
In the end, learn some tips and tricks for Vim:
Want even more Vim? Opt for this Vim course:
Mastering Vim Quickly
Master Vim like a true professional with this highly rated, premium Vim book and training course.
I hope you will find this guide helpful.