Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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)

share|improve this question
up vote 6 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
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

I set the numberwidth to, say, 5 and then set the columns to 85 (80+5) That should be good for files from 0 lines to a million lines.

Here is what you may have to put in (default) ~/.vimrc file

set numberwidth=5  "Good for files upto a million lines
set columns=85
set nu

You will see line number are formatted with '%5d' (5 is the numberwidth above) instead of default '%d' format, wasting a bit of real-estate before the line numbers, but maintaining consistency.

share|improve this answer
Welcome to Super User. Please be aware you have posted an answer to a question that is very old. Although there is nothing wrong with doing so, just be aware you may not get a response. – CharlieRB Jun 3 at 18:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .