Wikia

Vim Tips Wiki

Changes: Resize window when showing/hiding line numbers

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 27: Line 27:
 
endif
 
endif
 
set number!
 
set number!
if &number
+
if &number
 
exec "set columns+=" . g:grow
 
exec "set columns+=" . g:grow
 
else
 
else
Line 33: Line 33:
 
endif
 
endif
 
endfunction
 
endfunction
map <M-n> :call Toggle_num()<CR>
+
map <M-n> :call Toggle_num()<CR>
 
</pre>
 
</pre>
   
Line 48: Line 48:
   
 
<pre>
 
<pre>
let g:grow = &amp;numberwidth
+
let g:grow = &numberwidth
 
</pre>
 
</pre>
   

Latest revision as of 23:41, September 29, 2008

Tip 995 Printable Monobook Previous Next

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.

CommentsEdit

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

Around Wikia's network

Random Wiki