Vim is a powerful and highly configurable command line editor that comes installed with most Linux operating systems. It offers many useful features for editing and configuration of files. However, some of its useful features are disabled by default. One of them is line numbering. With Vim line numbering features, you can display line numbering at the beginning of each line which comes helpful when modifying the text. Line numbers are also useful in debugging scripts, code reviews, and configuration files. By default, line numbering is disabled.

Vim has the following three line numbering modes:

  • Absolute line number
  • Relative line number
  • Hybrid line number

This article explains how to show or hide line numbers in Vim editor. We will discuss all three modes.

Note:

  • Use the Ctrl Alt T keyboard shortcut to open the command line Terminal.
  • We have tested the commands and procedure on Ubuntu 22.04 LTS OS. The same commands and procedures are valid for any Linux distribution.

Show Absolute Line Numbers in Vim Editor

It is the standard line numbering mode which shows the line numbers in the beginning of each line in the file.

To show absolute line numbering, follow the below steps:

1. Switch to Normal mode by hitting the Esc key.

2. Then hit : and type the below command and hit Enter.

set number

or you can use the below abbreviation after hitting the “:” key:

set nu

Now you will see the absolute line numbers at the start of each line.

<img alt="Show Line Numbers in Vim Editor" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1255.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="408" src="data:image/svg xml,” width=”699″>

Hide Absolute Line Numbers

To hide absolute line numbering, follow the below steps:

1. Switch to Normal mode by hitting the Esc key.

2. Then hit : and type the below command and hit Enter.

set nonumber

or you can use the below command after hitting the “:” key and hit Enter:

set number!

You can also use the abbreviated form of the above commands set nonu or set nu! after pressing the : to hide absolute line numbers.

Show Relative Line Numbers in Vim Editor

In relative line numbering mode, the current line is marked as 0 while all the above and below lines are incrementally numbered (1,2,3,…) relative to the current line.

To show relative line numbering, follow the below steps:

1. Switch to Normal mode by hitting the Esc key.

2. Then hit : and type the below command and hit Enter.

set relativenumber

or you can use the below abbreviation after hitting the “:” key:

set rnu

Now you will see the current line number marked as 0 and if you move the cursor up or down, you will see the numbers marked as 1,2 3,….

<img alt="Show Relative Line Numbers in Vim" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1256.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="463" loading="lazy" src="data:image/svg xml,” width=”781″>

Hide Relative Line Numbers

To hide absolute line numbering, follow the below steps:

1. Switch to Normal mode. Hit the Esc key to do so:

2. Then hit : and type the below command and hit Enter:

set norelativenumber

or you can use the below command after hitting the “:” key and hit Enter:

set relativenumber!

You can also use the abbreviated form of the above commands set nornu or set rnu! after pressing the : to hide relative line numbers.

Show Hybrid Line Numbers in Vim

Hybrid line numbering enables both absolute and relative line numbering. It is similar to the relative line numbering except the current line shows its absolute number instead of showing 0.

To show relative line numbering, follow the below steps:

1. Switch to Normal mode by hitting the Esc key.

2. Then hit : and type the below command and hit Enter.

set number relativenumber

or type the below commands one by one after hitting the “:” key for once:

set number

set relativenumber

<img alt="Show Hybrid Line Numbers in Vim" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1257.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="463" loading="lazy" src="data:image/svg xml,” width=”781″>

Hide Hybrid Line Numbers

To hide absolute line numbering, follow the below steps:

1. Switch to Normal mode by hitting sthe Esc key.

2. Then hit : and type the below command and hit Enter:

set nonumber norelativenumber

or type the below commands one by one after hitting the “:” key for once:

set number!
set relativenumber!

You can also use the abbreviated form of the above commands set nonu nornu or set nu! set rnu!(one by one) to hide absolute line numbers.

Enable File Numbering in Vim Permanently

The method we have discussed above only enables the line numbering for the currently opened file. To enable line numbering for all the files that opens in Vim, follow the below procedure:

1. Edit .vimrc configuration file using the below command in Terminal:

$ vim ~/.vimrc

2. Then in the insert mode, use the following commands:

To enable absolute line numbering mode, the entry would be:

set number

To enable relative line numbering mode, the entry would be:

set relativenumber

To enable hybrid line numbering mode, the entry would be:

set number relativenumber

<img alt="Vim Editor" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/09/echo/word-image-1258.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="325" loading="lazy" src="data:image/svg xml,” width=”616″>

Then press Esc and type :wq to save and exit the file.

That is all there is to it! By following the procedures discussed in this article, you can show or hide line numbers in Vim. You have learned about different line numbering modes and how to enable them for a current file or permanently for all files.