Besides regular absolute line numbers, Vim has relative and “hybrid” modes to help navigate around files. With some autocommands, Vim can even toggle between line number modes automatically when you switch files or enter insert mode.
Absolute line numbers
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 numbers
Besides being useful for finding a line from a stacktrace or a test result, you can use line numbers to help you quickly jump around. For example, if you want to go to the tenth line in your file, you’d type esc if you’re in insert mode and then
:10 to move to the correct line.
Relative line numbers
relativenumber option, each line in your file is numbered relative to the line you’re currently on. 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 numbers
Like any any command 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.
However, it can be difficult to see how many lines 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 lenghy files.
With relative line numbers, you can quickly 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, you can enable
relativenumber at the same time, to give you hybrid line number mode. All lines will show their relative number, except for the line you’re currently on, which will show its absolute line number.
:set number relativenumber :set nonumber norelativenumber " turn hybrid line numbers off :set !number !relativenumber " toggle hybrid line numbers
While not strictly relative, hybrid 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.
Automatic toggling between line number modes
Although relative line numbers are helpful when moving around in normal mode, 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 seperate 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 END
In 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.
ruleroption, 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