Vim doesn’t show line numbers by default, but they can be turned on in your configuration. Besides regular absolute line numbers, Vim has relative and “hybrid” modes to help navigate around files. With some autocommands, it can even toggle between line number modes automatically when you switch files or enter insert mode.
Absolute line numbers
Using the number option, Vim sets up absolute line numbers to show the line
number for each line in the file you’re working on.
:set number
:set nonumber " turn line numbers off
:set number! " toggle line numbersBesides being useful for finding a line from a stack trace or a test result, you
can use line numbers to help you jump around the file. For example, if you want
to go to the tenth line in your file, you’d type esc while in insert mode
and then :10 to move to the correct line.
Relative line numbers
With the relativenumber option, each line in your file is numbered relative
to the cursor’s current position to show the distance to that line. The
current line is marked 0, the ones above and below it are marked 1, and so on.
:set relativenumber
:set norelativenumber " turn relative line numbers off
:set relativenumber! " toggle relative line numbersLike most commands in Vim, the j and k (or ↑ and ↓) keys can be prefixed with a number. Typing 5j will move the cursor five lines down, for example. If you want to remove seven lines, you can use d7d without having to switch to visual mode to select the lines first.
It can be difficult to see how far you need to jump to get to where you want to be, rendering this method useless for any jumps over ten lines. Also, absolute line numbers become less useful when working in lengthy files.
With relative line numbers, you can glance at the number of the line you want to jump to. If it’s thirteen lines up, you press 13k to get there. To get back to where you came from, you use 13j.
“Hybrid” line numbers
Since Vim 7.4, enabling number and relativenumber at the same time produces
hybrid line number mode. All lines will show their relative number, except
for current line, which will show its absolute line number.
:set number relativenumber
:set nonumber norelativenumber " turn hybrid line numbers off
:set !number !relativenumber " toggle hybrid line numbersHybrid line numbers are what relative line numbers should have been. Instead of having a useless zero on the current line, it uses that space to give you an idea of where you are in the file.[1]
Automatic toggling between line number modes
Relative line numbers are helpful when moving around in normal mode, but absolute line numbers are more suited for insert mode. When the buffer doesn’t have focus, it’d also be more useful to show absolute line numbers. For example, when running tests from a separate terminal split, it’d make more sense to be able to see which test is on which absolute line number.
Using some autocommands, Vim can switch between line number modes automatically.
:set number relativenumber
:augroup numbertoggle
: autocmd!
: autocmd BufEnter,FocusGained,InsertLeave * set relativenumber
: autocmd BufLeave,FocusLost,InsertEnter * set norelativenumber
:augroup ENDIn this example, both absolute and relative line numbers are enabled by default, which produces “hybrid” line numbers. When entering insert mode, relative line numbers are turned off, leaving absolute line numbers turned on. This also happens when the buffer loses focus, so you can glance back at it to see which absolute line you were working on if you need to.
I’ve been using this way of automatically switching line number modes for
years. You can set it up yourself by putting the snippet above in your
~/.vimrc, or use the
vim-numbertoggle plugin,
which does the same thing.
ruler option, which shows the current line and column. You can see it in action in the screenshots above, in the bottom right. It’s turned on by default in NeoVim and vim-sensible, but you can also set turn it on yourself using :set ruler.