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
  set number!
  if &number
    exec "set columns+=" . g:grow
    exec "set columns-=" . g:grow
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.

Comments[edit | edit source]

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


let g:grow = &numberwidth

Community content is available under CC-BY-SA unless otherwise noted.