Introduction
Vim does not display line numbers by default. However, the numbers that mark the beginning of each line may be useful to modify text with the Vim command. Many of these commands require you to specify line numbers.
In this tutorial, learn how to show or hide line numbers in Vim / VI in Linux.
Note: If you don't have Vim installed, read our tutorials on How to Install Vim on Ubuntu or How to Install Vim on CentOS.
3 Options to Show Line Numbers in Vim
There are three (3) different ways to display line numbers in Vim / Vi. You can set the text editor to show:
- Absolute line numbers: Represents the well-known chronological numbering.
- Relative line numbers: Each line number is defined by its distance from the position of the cursor.
- Hybrid line numbers: A combination of the two above-mentioned options.
Display Absolute Line Numbers
1. First, switch to command mode by pressing Esc
.
2. Then, press :
to prompt a command line at the bottom of the file.
3. Finally, to show line numbers, type:
set number
Alternatively, you can use the abbreviated command:
set nu
Hide Absolute Line Numbers
1. Press Esc
to move to command mode.
2. Use :
to start the command bar.
3. Hide line numbers by typing in the following command:
set nonumber
If you prefer, you can also use its abbreviated form:
set nu!
Display Relative Line Numbers
1. Ensure you are in command mode by pressing Esc
.
2. Next, press :
and prompt a command line.
3. Set Vim to display relative line numbers with the command:
set relativenumber
Or, use its shortened form:
set rnu
When you display relative numbers in Vim, each line is numbered by its distance from the cursor.
Take a look at the position of the cursor in the image above. This position is marked as 0. Accordingly, lines above and under the cursor are labeled as 1, the next ones 2 and so on.
Turn off Relative Line Numbers
1. Switch to normal mode with Esc
.
2. Press :
.
3. Now you can turn off relative line numbers by typing one of the following commands:
set nonumber norelativenumber
set nonu nornu
Display Hybrid Line Numbers
1. Use Esc
to make sure you are in command mode.
2. Open the command line in Vim by pressing :
.
3. Activate hybrid line numbering by typing:
set number relativenumber
Hybrid line numbering is a combination of absolute and relative numbering. It uses relative line numbering for everything except the cursor line, which is marked by its absolute number instead of zero.
Turn off Hybrid Line Numbers
1. Move to command mode (Esc
).
2. Start by typing :
.
3. Then, deactivate hybrid line numbering with the command:
set nonumber norelativenumber
Permanently Enable Vim Line Numbers
Vim doesn’t include line numbering by default. Activating line numbering only applies to the file you are working in. Once the file is closed, Vim returns to hiding line numbers.
1. To enable line numbers permanently, open the Vim configuration file under the name .vimrc
.
vim ~/.vimrc
2. Add the following line to the file:
:set [prefered_numbering_mode]
Note: If you do not have a .vimrc file in your home directory, create one with vim .vimrc
.
Conclusion
In this tutorial, you learned how to display or hide line numbers in the Linux Vim text editor.
Vim allows many user-friendly modifications; adding line numbers is just one of them. To go a step further, find out how to use vim color schemes.
Next you should also read
SysAdmin,DevOps and Development
How to Save a File in Vi / Vim & Exit
April 11, 2019
Vim (Vi IMproved) is a well-known, open-source text editor for Linux or Unix systems. It is a powerful and…
SysAdmin,DevOps and Development
22 Best Linux Text Editors for Programming & Coding
September 3, 2019
A text editor is an application that lets you type text. All Linux distributions come with built-in editors,…
SysAdmin,DevOps and Development
How to Delete Line in Vim on Linux
April 22, 2020
Vim allows you to delete entire lines, words or characters using various Vim commands. Learn the most…
SysAdmin,DevOps and Development
How to Undo and Redo Changes in Vim / Vi
April 28, 2020
Mastering basic Vim commands includes learning how to undo and redo changes in this text editor. Knowing how…
Author
Sofija Simic
Sofija Simic is an aspiring Technical Writer at phoenixNAP. Alongside her educational background in teaching and writing, she has had a lifelong passion for information technology. She is committed to unscrambling confusing IT concepts and streamlining intricate software installations.