Show & Hide Line Numbers in Vim / Vi

Amber Chen
Amber ChenSenior Engineer,, Lyft
Published: January 31, 2024

The Short Answer

In vim, display or showing line numbers is synonymous with setting line numbers:

  • set number or set nu will show or turn on line numbers
  • set nonumber or set nonu will hide the line numbers
  • set relativenumber or set rnu will show line numbers relative to the cursor position
  • set nonumber norelativenumber or set nornu will hide line numbers
  • set number! will toggle the line numbers on/off
  • set relativenumber! will toggle the relative line numbers on/off

Showing Absolute Line Numbers

For long files, it can be easy to lose track of the code in question, but having line numbers can be a useful point of reference.

Use the command, set number to show the absolute line numbers in the editor.

Showing Relative Line Numbers

It’s possible to also view line numbers relative to the current line where the cursor is located. This is used less commonly than absolute line numbers but can be helpful when copy and pasting a code block.

Use the command, set relativenumber to show the relative line numbers in the editor.

Toggling Line Numbers

To toggle between showing and hiding line numbers, add a bang ! to the end of the command.

  • set number! for absolute line numbers
  • set relativenumber! for relative line numbers

Showing Line Numbers by Default

Edit the ~/.vimrc file by using this command: vim ~/.vimrc

Then add set number to the file and save and quit. Then any new vi editors will show absolute line numbers by default.

What’s Next?

Now that you know how to show line numbers, you may be wondering how to jump to a specific line number. Read more tips and tricks on vim in our collection of posts about vim.

Written by
Amber Chen
Amber ChenSenior Engineer,, Lyft
Filed under

Related articles


Go To End of File in Vim / Vi

Select and delete to the end of a file in Vim

Select all in Vim / Vi

Select all and copy, paste, or delete the contents

Copy & Paste in Vim / Vi

Copy (Yank), Paste (Put) and Cut (Delete) in Vim

Searching in Vim

Search forward, backward, case insensitive, and more

Undo & Redo in Vim / Vi

Keyboard shortcuts and summary of how Vim tracks changes

Vim Go To End/Start of Line

Go to the end or beginning of a line in Vim

Go To Line In Vim

Variety of approaches to go to lines

Go To Top of File in Vim

Learn how to jump the top of the file in Vim, then navigate, search, highlight, and delete.

Vim Modes

Learn about seven of Vim’s modes

Vim / Vi Page Up and Down Controls

Quick reference for Vim's page up and down controls

How to delete lines in Vim / Vi

The best answer depends on which mode you use

Vim Find And Replace

You can use either the substitute command or the slash and dot method to search for patterns and replace with different values.