How to Show or Hide Line Numbers in Vim

February 28, 2022

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.

Tutorial on how to display line numbers in Vim or vi text editor

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.

Show Line Numbering 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
Showing line number in Vim text editor in linux

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!
Hide line number in Vim.

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
Displaying an example of vscode vim relative line numbers

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
Vim set hybrid line numbers command screenshot example

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 Show Line Numbers in Vim

Since 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 number

3. Save and exit the file by pressing Esc to switch to command mode and type:

:wq

By default, this enables absolute line numbering.

Note: If you do not have a .vimrc file in your home directory, create one with vim .vimrc.

Additional Settings for Showing Line Numbers in Vim

Read on to learn some additional options Vim offers for showing line numbering.

Change Column Width for Numbering

Vim versions 7 and newer include the option of modifying the width of the gutter column used for numbering.

To change the column width, switch to command mode with Esc and type:

:set numberwidth=[number]

To set the gutter column width permanently, use the same syntax and add the line to the .vimrc file.

For instance, to expand the width to have the value 10, you would use:

:set numberwidth=10
Change column width for numbering.

Change Text Wrapping for the Number Column

To configure text wrapping for the number column, press Esc and run:

:set cpoptions+=n

Alternatively, add the options to the ~/.vimrc file:

set cpoptions+=n

Change Line Number Color

To change the color of the numbers displaying line numbering, press Esc and run:

highlight LineNr term=bold cterm=NONE ctermfg=[color] ctermbg=NONE gui=NONE guifg=[color] guibg=NONE

For example, to change the line numbering to red, you would type:

highlight LineNr term=bold cterm=NONE ctermfg=Red ctermbg=NONE gui=NONE guifg=Red guibg=NONE
Change line number color.

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.

Was this article helpful?
YesNo
Sofija Simic
Sofija Simic is an experienced Technical Writer. 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.
Next you should read
How to Save a File in Vi / Vim & Exit
September 22, 2022

Vim (Vi IMproved) is a well-known, open-source text editor for Linux or Unix systems. It is a powerful and...
Read more
22 Best Linux Text Editors for Programming & Coding
August 8, 2024

A text editor is an application that lets you type text. All Linux distributions come with built-in editors...
Read more
How to Delete Line in Vim on Linux
September 26, 2024

Vim allows you to delete entire lines, words or characters using various Vim commands. Learn the most...
Read more
How to Undo and Redo Changes in Vim / Vi
October 10, 2024

Mastering basic Vim commands includes learning how to undo and redo changes in this text editor. Knowing how...
Read more