Toggle line number mode

3.4
(195)

Relative line numbering can facilitate intra-buffer navigation by making it easier to know how far away you’d like to jump. Performing operations over a number of lines becomes much easier when you can glance at a number instead of doing math in your head. But absolute line numbers are a must have for me – I’ve become quite accustomed to seeing them most of the time. And if you are ever screen sharing or pairing, absolute line numbers are essential for talking about code. We have covered the line numbering topic before, though some of you have very strong opinions about it.

But here’s a trick that improves my efficiency in Vim. When line numbering is already active using :set number, relative numbering can be toggled by using :set relativenumber! or :set rnu! for short.

I found this useful enough to add a keyboard mapping to more seamlessly adopt it in my workflow. By using the following in my .vimrc, I can quickly toggle between relative and absolute line numbering.

<leader>rn :set rnu!<cr>

Now I can use <leader>rn, which in my case means <space>rn, to toggle relative line numbers off and on.

How useful was this tip?

Average rating 3.4 / 5. Vote count: 195

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

81 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts

Leave a reply

Your email address will not be published. Required fields are marked *