Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I use gvim with

set number

in my .vimrc so it shows the linenumber, plus a space, before every line.

If the document has less than 10 lines, this takes 2 columns, If it has more than 10, 3 columns and so on.

If I put

set lines=40 columns=80

in .gvimrc, it will show 78 columns of code and 2 for the linenumber plus pace, or 77 + 3 and so on.

I'd like the window width to be set to 80 columns + whatever number of columns are necessary to show the linenumber.

It doesn't need to be dynamic, but I'd like to be able to read the number of lines of a given file and set columns according. (Of course, a dynamic solution would be really neat, but not essential)

Thanks!

share|improve this question

2 Answers 2

up vote 5 down vote accepted

Try this in your ~/.vimrc:

au BufRead * let &numberwidth = float2nr(log10(line("$"))) + 2
          \| let &columns = &numberwidth + 80

Every time you load a buffer (i.e., open a file), that will determine the number of columns required to show the largest line number, set the 'numberwidth' option accordingly, and set 'columns' to that number plus 80 for your text.

share|improve this answer
    
Thanks! It works great when opening a file. Any idea how to make this happen when switching tabs too? Thanks in advance. –  Elton Carvalho Sep 1 '11 at 11:56
1  
Vim's autocommand events are listed at :help autocommand-events. Among them is TabEnter, so you could try replacing BufRead by BufRead,TabEnter in the example above and see if that gives you the behavior you want. I have not tested the example with that event myself. –  garyjohn Sep 1 '11 at 15:34
    
Thank you @garyjohn, TabEnter worked as expected. Maybe later I will play with those autocommand-events. They are eally useful! –  Elton Carvalho Sep 2 '11 at 9:40

I use

set lines=24 columns=84

in my $HOME/.gvimrc which gives me what you want. I rarely (if ever) have files that are longer the 999 lines so it works for me.

share|improve this answer
    
Thats what I was doing, but keeping exactly 80 columns of text is nice because I can prevent ending up with 81 chars in one line, which is no big deal, but is nice to avoid. –  Elton Carvalho Sep 1 '11 at 11:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.