Show & Hide Line Numbers in Vim / Vi

Show & Hide Line Numbers in Vim / Vi

[#short-answer]The Short Answer[#short-answer]

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

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

[#showing-absolute-line-numbers]Showing Absolute Line Numbers[#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, [.inline-code]set number[.inline-code] to show the absolute line numbers in the editor. 

[#showing-relative-line-numbers]Showing Relative Line Numbers[#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, [.inline-code]set relativenumber[.inline-code] to show the relative line numbers in the editor. 

[#toggling-line-numbers]Toggling Line Numbers[#toggling-line-numbers]

To toggle between showing and hiding line numbers, add a bang [.inline-code]![.inline-code] to the end of the command.

  • [.inline-code]set number![.inline-code] for absolute line numbers
  • [.inline-code]set relativenumber![.inline-code] for relative line numbers

[#showing-line-numbers-by-default]Showing Line Numbers by Default[#showing-line-numbers-by-default]

Edit the [.inline-code]~/.vimrc[.inline-code] file by using this command: [.inline-code]vim ~/.vimrc[.inline-code]

Then add [.inline-code]set number[.inline-code] 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.