How to show line numbers in Vim
Type :set number and press ↵ to show line numbers; :set nonumber hides them. :set relativenumber numbers each line by its distance from the cursor instead.
| Type | What it does |
|---|---|
:set number ↵ | Show line numbers. Short form: :set nu. |
:set nonumber ↵ | Hide them (:set nonu). |
:set number! ↵ | Toggle: on if they are off, off if they are on (:set nu!). |
:set relativenumber ↵ | Number each line by how far it is from the cursor; the cursor's line shows 0 (:set rnu). |
:set number relativenumber ↵ | Both: the cursor's line shows its real number, the others their distance. |
:set nonumber norelativenumber ↵ | Turn both off. |
:set number? ↵ | Show whether the option is on. |
| Ctrl-g | Show the file name, its number of lines and how far through it you are. |
| gCtrl-g | Show the column, line, word and byte you are on, out of the totals. |
Numbers from memory
Type each command as you would in Vim, Enter included, with the option names spelled out in full unless the prompt says otherwise.
Why relative numbers
Vim's moves take counts: 5j goes five lines down, d3k deletes this line and the three above it. With relativenumber on, the gutter shows that count beside every line, so you read it instead of working it out. Most people turn on both options. Then the cursor's line shows its real line number, for error messages and :42, and every other line shows how far away it is, for j and k.
Keep them on
:set lasts until you close Vim. To have numbers every time, put the setting in your config file without the colon. For Vim that is ~/.vimrc:
set number relativenumber
For Neovim it is ~/.config/nvim/init.lua, in Lua:
vim.opt.number = truevim.opt.relativenumber = true
Restart the editor, or reload the file with :source % while you are editing it.