How to Show Line Numbers in Vim and Vi

By 

Updated on

4 min read

Vim Line Numbers

When editing code, configuration files, or scripts in Vim, line numbers make it easier to jump to a specific line, review errors, and discuss changes with another person.

Vim does not show line numbers by default. You can enable them for the current session with :set number, or make them permanent by adding set number to your .vimrc file.

This guide explains how to show, hide, toggle, and save line number settings in Vim and Vi.

Quick Reference

For a printable quick reference, see the Vim cheatsheet .

TaskCommand
Show absolute line numbers:set number
Hide absolute line numbers:set nonumber
Toggle absolute line numbers:set number!
Show relative line numbers:set relativenumber
Hide relative line numbers:set norelativenumber
Show hybrid line numbers:set number relativenumber
Check current absolute setting:set number?
Make absolute line numbers permanentAdd set number to ~/.vimrc

Absolute Line Numbers

Absolute line numbering displays the actual line number next to each line of text.

To activate the line numbering, set the number flag:

  1. Press the Esc key to switch to normal mode.

  2. Press : (colon) and the cursor will move at the bottom left corner of the screen. Type set number or set nu and hit Enter.

    vimrc
    :set number
    Vim enable line numbers
  3. Line numbers will be displayed at the left side of the screen:

    Vim show line numbers

To disable absolute line numbers, run the :set nonumber or set nonu commands:

vimrc
:set nonumber

You can also toggle the line numbers with :set number! or :set nu!:

vimrc
:set number!

Relative Line Numbers

When the relative line numbering is enabled, the current line is shown as 0. The lines above and below from the current line are incrementally numbered (1, 2, 3, etc.).

Relative line numbers are a Vim and Neovim feature and are not available in classic vi.

Relative line mode is handy because many Vim operations, such as moving up/down and deleting lines work on relative line numbers.

For example, to delete the next ten lines below the cursor, you would use the d10j command. With relative line numbers enabled, you have a better visual overview of the code.

To enable the relative line numbering, switch to the command mode and enter :set relativenumber or :set rnu:

vimrc
:set relativenumber
Vim relative line numbers

To disable the relative line numbering, type :set norelativenumber or set nornu:

vimrc
:set norelativenumber

To toggle the relative line numbering, use the :set relativenumber! or :set rnu! command:

vimrc
:set relativenumber!

You can check the current setting with:

vimrc
:set relativenumber?

Hybrid Line Numbers

In Vim 7.4 and later, enabling both the absolute and relative line numbers at the same time sets up the hybrid line number mode.

Hybrid line numbering is the same as the relative line numbering with the only difference being that the current line instead of showing 0 shows its absolute line number.

To turn on the hybrid line numbering, run both the number and relativenumber commands:

vimrc
:set number relativenumber

The short version is:

vimrc
:set nu rnu
Vim hybrid line numbers

The same can be achieved by running the commands one by one:

vimrc
:set number
:set relativenumber

To disable hybrid mode, turn off both the absolute and relative numbering.

Make Line Numbers Permanent

Commands such as :set number apply only to the current Vim session. To show line numbers every time you open Vim, add the setting to your .vimrc file.

Open the Vim configuration file:

Terminal
vim ~/.vimrc

Add this line to enable absolute line numbers by default:

~/.vimrcvim
set number

If you prefer relative line numbers, add:

~/.vimrcvim
set relativenumber

For hybrid numbering, where the current line shows its absolute number and nearby lines show relative numbers, add both settings:

~/.vimrcvim
set number
set relativenumber

To toggle line numbers with a key, add a mapping like this:

~/.vimrcvim
nnoremap <F2> :set number!<CR>

Conclusion

Use :set number when you need absolute line numbers for the current Vim session. Add set number to ~/.vimrc when you want Vim to show line numbers every time it starts.

Tags

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 800+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page