Introduction
Before we begin talking about how to show line numbers in Vim / Vi, let's briefly understand – What is Vim?
Many software engineers and Linux system administrators prefer Vim/Vi as their text editor of choice.
Vim does not display line numbers by default, but they can be easily enabled. Vim has three different line numbering options to help you browse through your files. Vim offers relative and hybrid line numbering in addition to the traditional absolute line numbering.
Line numbering can be beneficial in a variety of contexts, including pair programming, debugging scripts, code reviews, referencing to a specific line, and more.
This tutorial will show you how to use the Vim / Vi text editor to show or hide line numbers. We will also address a few FAQs on how to show line numbers in Vim / Vi.
Absolute Line Numbers
Absolute line numbering is the most common type of line numbering, in which the relevant line number is displayed next to each line of text.
Set the number flag, to enable line numbering:
- To enter command mode, press the
Esc
key. - The pointer will travel to the bottom left corner of the screen if you press
:
(colon). HitEnter
after typingset number
orset nu
.
:set number
3. Line numbers will appear on the left side of the screen:
Run the :set nonumber
or set nonu
commands to turn off absolute line numbers:
:set nonumber
You can also use the command :set number!
to toggle the line numbers or set nu!
:
:set number!
Relative Line Numbers
The current line is presented as 0 when relative line numbering is enabled; the lines above and below the current line are gradually numbered (1
, 2
, 3
, etc.).
Because many Vim operations, such as moving up/down and deleting lines, rely on relative line numbers, relative line mode is useful.
The d10j
command, for example, can be used to remove the next ten lines below the cursor. You'll get a better visual overview of the code if relative line numbers are enabled.
Switch to command mode and type :set relativenumber
or :set rnu
to enable relative line numbering.
:set relativenumber
Use :set norelativenumber
or set nornu:
to deactivate relative line numbering.
:set nonumber
Use the :set relativenumber!
or : set rnu!
command to enable or disable relative line numbering :
:set number!
Hybrid Line Numbers
The hybrid line number mode is enabled in Vim 7.4 and later by enabling both absolute and relative line numbers at the same time.
The sole difference between hybrid and relative line numbering is that instead of displaying 0
on the current line, it displays its absolute line number.
Run both the number
and relativenumber
commands to enable hybrid line numbering:
:set number relativenumber
Running the commands one by one achieves the same result:
:set number
:set relativenumber
To turn off the hybrid mode, you must disable both absolute and relative numbering.
Permanent Settings
Add the necessary command to your .vimrc
if you want line numbers to appear every time you run Vim (Vim configuration file). To enable absolute line numbering, for example, use the following:
vim ~/.vimrc
:set number
sudo service nginx stop
FAQs on How to Start, Stop and Restart Nginx
How can I display line numbers in Vim?
Use the command :set number
to display line numbers on the left side of the editor window.
How can I toggle line number display on and off in Vim?
Press Ctrl + g
in command mode to toggle the display of line numbers.
Is it possible to display both absolute and relative line numbers simultaneously in Vim?
Yes, you can use the :set number relativenumber
command to display both absolute and relative line numbers.
Can I hide relative line numbers while keeping the absolute line numbers visible in Vim?
Yes, you can use the :set norelativenumber
command to hide relative line numbers while keeping absolute line numbers visible.
How can I highlight the current line in Vim?
Use the command :set cursorline
to highlight the current line in Vim.
How can I show a vertical line at a specific column in Vim?
You can use the :set colorcolumn=<column_number>
command to display a vertical line at the specified column.
How can I show a line at the bottom of the Vim editor window as a status line?
Use the command :set laststatus=2
to show a status line at the bottom of the Vim editor window permanently.
Conclusion
Use the :set number
command to display absolute line numbers, and the :set relativenumber
command to display relative line numbers in Vim. Vim switches to the hybrid line numbering mode if both absolute and relative line numbers are enabled.
If you have any queries, please leave a comment below and we’ll be happy to respond to them.