Vim/Vi is a popular text editor for many Linux software developers and system administrators.
It’s especially true if you plan to debug any program errors or improve the script or program’s overall readability.
Today, we’ll teach you how to display line numbers when you use vim/vi.
Show or Hide Vim Line Numbers
By default, Vim/Vi number lines aren’t displayed when you use the text editor. Nonetheless, there’s a way for you to hide and display line numbers in vim whenever you want to.
Today’s tutorial is straightforward, and you’ll have nothing to worry about! You don’t need root privileges, and the only thing you’ll need to equip yourself with is Vim.
Absolute VIM Line Numbers
The standard for line numbering in Vim is the absolute line number. It will show the line number beside the following line.
Display VIM Absolute Numbers
If you want to activate and show the Vim line numbering, set the number flag, and follow these easy steps.
Step 1: Press “ESC” to go to the command mode.
Step 2: Press “:” or colon, so that the cursor goes to the bottom left of your screen.
Step 3: Type either two of the following scripts:
:set number OR :set nu
Step 4: Press “ENTER.” From there, the line numbers should be displayed on the left side of your screen.
Turn-off VIM Absolute Numbers
If you want to turn off vim absolute line number, type the following command on your script:
:set number OR set nonu
Toggle VIM Line Numbers
You can even toggle line numbers using the following command:
:set number! OR :set nu!
Relative Line Numbers
When you enable relative line numbers, the current line numbering will be displayed as 0.
The lines above and below the previous lines will then display an incremental number.
Enabling the relative line number is pretty helpful! There’s also a ton of operations in Vim where you’ll be working with relative numbers.
Let’s set an example. If you want to remove the next ten lines under your cursor, what you’d usually do is type the “d10j” command.
When you enable relative line numbers, you’ll have a better visual and overview of the next ten lines you want to remove.
Show Relative Line Numbers
Enabling relative line numbers is easy. Open your command mode and type the following command:
:set relativenumber OR :set rnu:
Disable Relative Line Numbering
Disabling relative line numbers is also easy. Type any of the following commands, “SET NONUMBER,” “SET NORNU,” or “SET NORELATIVENUMBER”:
:set norelativenumber or set nornu: or :set nonumber
Toggle Relative Line Numbering
If you want to toggle relative line numbers, you can run either of the following commands:
:set relativenumber! or :set rnu! or :set number!
Hybrid Line Numbers
You can also set up the hybrid line mode to enable both relative and absolute line numbers.
But, this is only possible with Vim 7.4 or a later version of it.
Hybrid line numbering is nearly similar to relative numbering. The only difference is that the current line number shows an absolute line number in vim instead of 0.
Enable Hybrid Line Numbers
You can try this out yourself and enable hybrid line numbers in vim. Make sure to run both “NUMBER” and “RELATIVENUMBER” commands:
:set number or :set relative number
Disable Hybrid Line Numbers
The only way for you to disable Hybrid mode is by disabling both relative and absolute line numbers in vim.
You can even make the line numbers in vim appear every time you launch the software. The steps are easy.
To accomplish this, we just make a quick edit to the .vimrc, the Vim configuration file:
Next, don’t forget to append the following line:
Opening a file in a particular line number
Using the right command lets you jump to a specific line, when opening a particular file.
Enter the following command on your script:
vi +linenumber filename vi +300 filename
The next time you want to show line numbers in Vim, remember the steps we taught you!
Use the “:set number” command if you want to enable absolute line numbers; or “:set relativenumber” for relative Vi numbers.
If you want to turn off the line numbering, remember the command “set nonumber.”
When you have both absolute and, hybrid numbers enabled, you’ll have access to the hybrid mode!
We hope you learned a lot from this tutorial. Feel free to leave a comment below on how we were able to help you!
We’ll be glad to hear from you soon.