Vim is one of most popular and extensive text editors in the world of development and sys admins as well. It offers a set of tools and customization that almost every developer would need. Vim enables you to use key combinations to perform all the tasks.
However, Vim does not display line numbers by default. Line numbers are useful especially when working with a large file. It can allow you to jump and navigate to specific lines in a single command, commenting blocks of code, multi-line editing, and more.
Using this tutorial, you will learn how enable Vim to show line numbers using three modes: standard absolute numbering, relative, and hybrid numbering.
Let’s delve in into the world of Vim.
Enable Absolute Line Numbering in Vim
Absolute numbering is one of the most common line numbering format. This mode provides line numbering in the traditional chronological format. Each line number is displayed next to the start of the line.
To activate absolute line numbering in Vim, open the file to edit:
vim node1.py
- Start by pressing the
ESC
key to enable Vim Command Mode. - Next, press the
:
followed by the commandset number
and pressReturn
.
The command is as shown:
:set number
Or:
:set nu
Before:
After:
Vim will enable line numbers on the left side at the start of each line. Keep in mind that an empty line is treated a valid line.
To disable absolute line numbering, run the command:
:set nonumber
Or:
:set nonu
Or:
:set number!
Or:
:set nu!
Vim Enable Relative Line Numbering
In some cases, you may need to enable relative numbering mode in Vim. In relative mode, the current line is assigned the value 0
while other lines are assigned a value based on the distance from the current cursor position.
The format is as shown:
3
2
1
0 -- current line
1
2
3
To enable relative numbering, run the command:
:set relativenumber
Or:
:set rnu
Before:
After:
As you can see in the screenshot above, each line, before and after, the current line is assigned a value denoting the distance from the current cursor position.
To disable relative numbering, use the commands:
:set norelativenumber
Or:
:set nornu
Or:
:set nonumber
Vim Enable Hybrid Numbering Mode
In recent Vim versions, you can enable both hybrid numbering mode which combines both absolute and relative numbering system.
Hybrid mode works in a similar manner to relative numbering. However, instead of the current line being number 0
, it shows the absolute (actual) line number.
To enable hybrid mode, run the command:
:set number relativenumber
Or:
:set number
:set relativenumber
This should enable absolute and relative numbering.
Before:
After:
As you can see from the output above, Vim, uses the absolute number in the current line.
To disable hybrid number, disable absolute and relative numbering:
:set nornu
:set nonumber
Vim Enable Numbering by Default
Instead of enabling line numbering when editing a given file, you can enable line numbering in the Vim configuration file. This will enable Vim line numbering by default =.
Edit the vim config file:
vim ~/.vimrc
Add the numbering mode to the config file:
:set number
Save and close the file.
Conclusion
In this post, we discussed how to enable and disable line numbering modes using various commands. Choose the numbering mode you find comfortable and stick with it.
We hope you enjoyed this tutorial. Leave us a comment down below and share.
Vim over Emacs 😁!!
Thanks for reading and see you in the next one!!