Resize window when showing/hiding line numbers
From Vim Tips Wiki
created September 20, 2005 · complexity basic · author Craig Emery · version 5.7
When using
:set number
to show line numbers, your column alignment can be broken. However when you're in a window that supports it, the width of the window can be changed and as long as you've got enough screen real-estate to accommodate the extra columns your column alignment can be retained.
Add this to your vimrc file:
function! Toggle_num()
if !exists("g:grow")
let g:grow = 8
endif
set number!
if &number
exec "set columns+=" . g:grow
else
exec "set columns-=" . g:grow
endif
endfunction
map <M-n> :call Toggle_num()<CR>
So each time you use META-n, line numbers will be toggled and the appropriate number of columns will be added or removed from your vim window.
[edit] Comments
As of Vim 7.0, it's possible to change the line number column width using the numberwidth option. Of course, it doesn't really make sense now to always grow/shrink by 8 if numberwidth is set to, say, 4. So, an easy fix would be to change the line:
let g:grow = 8
into:
let g:grow = &numberwidth