Vim Tip #28: Toggle Line Numbers on and off


tags: Vim

Vim Tips

Sometimes it's incredibly useful to show line numbers in Vim - notably when you're trying to use a range of lines for a command. But when I don't need the numbers, I find them distracting and they take up precious screen space. You can turn them on with :setlocal number and then back off with :setlocal nonumber, but this is cumbersome and who's going to remember? The next step in speeding up this process is to understand that a lot of Vim's settings have toggles. In this case, :setlocal invnumber will change the state of line number display - from on to off, or vice versa. And this leads us to a mapping I use over and over every day:

nnoremap <leader>n :setlocal invnumber<CR>

(See Map Leader and Normal mode mappings if you're not familiar with <leader>.) Put the above line in your Vim configuration. This means that in normal mode I simply type -n (I use the Dvorak keyboard layout, so the dash is a good map leader) and the line numbers turn on - or off, if they're already on.