Vim / Vi is the preferred text editor for many software developers and Linux system administrators.
By default, Vim doesn’t display line numbers, but it can be easily displayed. Vim supports three numbering modes that help you navigate files. Aside from the standard absolute line numbering, Vim also supports relative and hybrid line numbering modes.
In this guide, we will show you how to show or hide line numbers in the Vim / Vi text editor.
Aside from assisting in navigating the code, viewing numbers online is also useful in other situations such as collaborative programming, script debugging, code review, ease of referencing specific lines and many more.
Absolute line numbers
Absolute line numbering is standard line numbering, and it displays consecutive line numbers next to each line of text.
To enable line numbering:
- push the button
EscTo switch to command mode.
:(2 dots) and the cursor will move in the lower left corner of the screen. Type
set nuAnd press
- Line numbers will be displayed on the left side of the screen:
To disable absolute line numbers, turn on
:set nonumber or
You can also replace line numbers with
:set number! or
Relative line numbers
When relative line numbering is enabled, the current line appears as
0, And the lines above and below the current line are gradually numbered (
3 … etc).
Relative positioning is useful because many operations in Vim such as move up / down and delete rows on relative line numbers.
For example, to delete the next ten lines below the cursor, you can use the command
d10j . With relative line numbers enabled, you will have a better visual overview of the code.
To enable relative line numbering, switch to command mode and enter
:set relativenumber or
To turn off relative line numbers, type
:set norelativenumber or
To activate relative line numbers, use the command
:set relativenumber! or
Hybrid numbering mode
In Vim 7.4 and later, enabling absolute and relative line numbers at the same time sets mixed line number mode.
Hybrid line numbering is the same as relative line numbering, the only difference being that the font is current instead of width
0 Instead, it displays the absolute row number.
To enable mixed line numbering, run the command
number And the
:set number relativenumber
The same can be accomplished by executing the commands one by one:
:set number :set relativenumber
To disable mixed mode, you must turn off absolute and relative numbering.
If you want the line number to appear every time you start Vim, add the appropriate command to
.vimrc (Vim configuration file). For example, to enable absolute line numbering, you can add the following: