Matt Kirman

Relative Line Numbers in Vim

I’ve been gradually moving back to Vim from and one aspect that has really swayed me is the recent addition of the relativenumber setting in Vim 7.3.

One thing that I used to be particularly guilty of was repeatedly hammering j and k rather than performing the mental arithmetic required to use the much shorter 37j. I had the same issues deleting or moving lines where switching into visual mode was quicker than counting lines and using d12d.

Relative line numbers in Vim

Going Relative

Vim now has a setting called relativenumber which acts in a similar way to the number setting. However, rather than calculating line numbers from the top of the file it shows them relative to the line you’re currently on.

I always disable the arrow keys in insert mode, to force myself to use 10j and keep my hands on the home row (plus it’s great for trolling). I prefer to use relative line numbers when in normal mode for ease of movement and absolute line numbers in insert mode. We can set this up with:

au InsertEnter * :set nu
au InsertLeave * :set rnu

Occasionally it’s useful to go to a specific line, such as when fixing failing tests. At first glance using relative line numbers appears to complicate the issue somewhat. But, line movement shortcuts such as <line-number>gg still work.