How to Show Line Numbers in Vim
Do you know Vim can show absolute, relative and hybrid line numbers? Here's how to set it up.
β Team LHB
Vim is a remarkable editor that most Linux/UNIX sysadmins have come to love and use every day.
You might think, βI would switch to Vim immediately if Vim could show me line numbersβ. Well, consider that done.
To display line numbers in Vim, go into the command mode by pressing Esc key and use:
:set number!
That !
part is necessary.
Easy, right? Let's see it in details and with examples.
Actually, you can show three kinds of line numbers in Vim:
- Absolute line numbers
- Relative line numbers
- Hybrid line numbers
Curious? Let's take a more in-depth look.
Show absolute line numbers in Vim
Absolute line numbers are what you find in almost all IDEs. It starts with 1 and ends with the number of the last line.
And, there are two ways to enable that.
Set option from an active Vim session
- Make sure that you are in the command mode. You can switch to the command mode by pressing the Escape (Esc) key.
- Press the ':' (colon) key. There should be colon character printed to the bottom left corner of Vim. Now type 'set number', or you can type 'set nu' (for short) and hit the 'Enter' key.
You should now see line numbers to the left side of the window.
For any reason, if you do not end up liking it, you can turn it off using the ':set nonumber' command or with 'set nonu' command for short.
Make it permanent by saving it in vimrc
If you want changes to be permanent, you can save your preferences in a '.vimrc' file that lives in your user's home directory.
- Open the '.vimrc' file using your favourite text editor -
vim ~/.vimrc
- Type 'set number' and ':wq' out of it.
Next time you open Vim, you will always see absolute line numbers in Vim.
If you no longer wish to have them, simply delete the line 'set number' from your user's '.vimrc' file.
Show relative line numbers in Vim
When you have relative line numbers enabled, it means that the line you are currently on is line number 0 and the lines above and below the cursor are consecutively numbered 1, 2, 3.
This is particularly useful because moving the cursor up and down x number of lines gets easier. You do not have to do the mental math of subtracting the current line number from the line number, and you can go to a specific line in Vim easily.
Showing relative line numbers in Vim from an active Vim session
- Make sure that you are in the command mode. You can switch to the command mode by pressing the Escape (Esc) key.
- Press the ':' (colon) key. There should be a colon character printed to the bottom-left corner of Vim. Now type 'set relativenumber', or you can type 'set rnu' (for short) and hit the Enter key.
You will see relative line numbers to the above and below the line that your cursor is on.
For any reason, if you do not end up liking it, you can turn off relative line numbering using the ':set norelativenumber' command or with 'set nornu' command for short. This will entirely disable the left column that shows the line numbers.
The same nifty trick applies for relativenumber
: If the relative line numbers are turned off, you can turn them on using ':set relativenumber!' or with ':set rnu!' command for short. If the relative line numbers are already toggled, setting this option will disable relative line numbers.
Always show relative line numbers in Vim
You tried relative line numbers, and you liked it. You are wondering how to make this change permanent, instead of typing ':set relativenumber' every time you open Vim.
To make this permanent, you need to set this option in your user's 'vimrc' file.
- Open the '.vimrc' file using Vim:
vim ~/.vimrc
- Type
set relativenumber
in '.vimrc', save and exit out of it.
Now, when you will open Vim, you will always have relative line numbers enabled.
In any case that you want to undo this behavior, you should remove the line that says 'set relativenumber' from the '.vimrc' file of your user.
Show hybrid line numbers
Hybrid line numbers is a mixture of absolute line numbers and relative line numbers. When the hybrid numbering mode is on, the line on which your cursor is at the moment will have absolute number while every other line will have relative numbers.
In my opinion, it is the best of both worlds where you want to know the line number that you are working on and also see the relative numbers up and below the current line.
Set option from an active Vim session
If you want to temporarily enable/disable hybrid line numbers, below are the steps to take so:
- Using commands requires you to be in the Normal mode, so make sure you are in the Normal mode.
- Start writing the command by pressing the ':' (colon) key. This will let Vim know that you are typing a command. Now type 'set number relativenumber'.
After toggling it, you will see that the line on which your cursor is on has an absolute line number and rest of the lines have a relative line number.
To disable hybrid mode, you need to disable both numbering by typing :set nonumber norelativenumber
.
Save preferences in vimrc
Once you try out hybrid mode and end up liking it, you want to set it as the default option. To do that, you need to set it in your user's vimrc file. Below are the steps to do that.
- Open the '.vimrc' file in your favorite editor -
vim ~/.vimrc
- Type
set number relativenumber
on a line and save it.
This will make the changes permanent.
Under any circumstance where you no longer want hybrid line numbering, simply remove the line set number relativenumber
from your user's 'vimrc'.
A side note on 'vimrc' file
Let's assume that your local username is 'happytux'. If you have specified your preferences in '/home/happytux/.vimrc', your preferences will be respected only if you open Vim as the user 'happytux'.
If you try 'sudo vim /etc/ssh/sshd_config', you are opening Vim as a super-user, which is not the user 'happytux', so in that case, you will not have your defaults reflected in the current Vim session.
Conclusion
When it comes to line numbers, Vim provides two additional modes which is suitable for Vim users. I like the hybrid line numbers because it helps me move faster in Vim while showing the actual line numbers.
If you are interested in learning more than just the Vim Basics, I highly recommend using this program by Jovica Ilic.
Team LHB indicates the effort of a single or multiple members of the core Linux Handbook team.