views:

9845

answers:

9

I feel like the way I do 80-column indication in Vim is incorrect: set columns=80. At times I also set textwidth but I like to be able to see and anticipate line overflow with the set columns alternative.

This has some unfortunate side effects -- I can't set number for fear of splitting between files that have different orders of line numbers; i.e. < 100 line files and >= 100 line files will require two different set columns values because of the extra column used for the additional digit display. I also start new (g)Vim sessions instead of splitting windows vertically, which forces me to use the window manager's clipboard -- vsplits force me to do set columns every time I open or close a pane, so starting a new session is less hassle.

How do you handle the 80-character indication when you want to set numbers, vertically split, etc.?

+2  A: 

I'm afraid that you've put constraints on the set of solutions that, well, leave you with the null set.

Using :set textwidth=80 will fix all of the problems you mentioned except that you can't easily see the line limit coming up. If you :set ruler, you'll enable the x,y position display on the status bar, which you can use to see which column you're in.

Aside from that, I'm not sure what to tell you. It's a shame to lose the number column, fold column and splits just because you have to :set columns=80.

Lucas Oman
Yeah, I was afraid of that... I was hoping there would be a hidden way to draw a thin vertical line like in more graphically oriented editors.
cdleary
+3  A: 

Newer versions of vim allow a :set numberwidth=x value, which sets the width of the line number display. I don't really use folding etc, so I wouldn't know about that though. Drawing a thin vertical line is beyond the abilities of a console application though. GVim may allow this (I don't use it, so can't comment there).

Matthew Scharley
+3  A: 

You can try this:

au BufWinEnter * if &textwidth > 8
\ | let w:m1=matchadd('MatchParen', printf('\%%<%dv.\%%>%dv', &textwidth+1, &textwidth-8), -1)
\ | let w:m2=matchadd('ErrorMsg', printf('\%%>%dv.\+', &textwidth), -1)
\ | endif

That will set up two highlights in every buffer, one for characters in the 8 columns prior to whatever your &textwidth is set to, and one for characters beyond that column. That way you have some extent of anticipation. Of course you can tweak it to use a different width if you want more or less anticipation (which you pay for in the form of loss of syntax highlighting in those columns).

Aristotle Pagaltzis
+54  A: 

I have this set up in my .vimrc:

highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%81v.\+/

This highlights the background in a subtle red for text that goes over the 80 column limit (subtle in GUI mode, anyway - in terminal mode it's less so).

Simon Howard
Great idea!I changed it to "ctermbg=darkred" and "guibg=#FFD9D9" to fit my needs better (light background in gvim and dark in cterm mode).
HS
I think a subtle correction is /\%81v.\+/ -- for some reason the .* highlights places where characters do not exist.
cdleary
This is working nicely. My code thanks you!
dwc
This made my day.
Casey
That is indeed quite brilliant!
Ali
Does this method account for tabs = 8 spaces? or does it count tab as one character?
@goathens: it highlights text that you see which exceeds 80 columns, so it depends on what you've set tabstop to. If tabstop=8, then it assumes a tab is 8 spaces.
Simon Howard
I think this line is slightly better for the match regex:match OverLength /\%>80v.\+/This doesn't highlight the 'end of line' character and also is more intuitive as you just set the number to what you want the line length to be, not +1.
David Terei
Didn't display anything at all until after I applied @cdleary's fix, now works beautifully.
meagar
Fix applied. Cheers :-)
Simon Howard
+5  A: 

match ErrorMsg '\%>80v.+'

Shorter way.

negative
+6  A: 

http://vim.wikia.com/wiki/VimTip810 Highlight long lines

kcwu
+1  A: 

Simon Howard's answer is great. But /\%81v.\+/ fails to highlight tabs that exceed column 81 . So I did a little tweak, based on the stuff I found on VIM wiki and HS's choice of colors above:

highlight OverLength ctermbg=darkred ctermfg=white guibg=#FFD9D9
match OverLength /\%>80v.\+/

And now VIM will highlight anything that exceed column 80. Cheers!

Z.Zen
+11  A: 

As of vim 7.3, you can use set colorcolumn=80 (set cc=80 for short).

Since earlier versions do not support this, my .vimrc uses instead:

if has("colorcolumn")
  set colorcolumn=80
else
  au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
endif

The online documentation has yet to be updated for vim 7.3. The only online source I've found for the 7.3 documentation is the repository, where the colorcolumn option is documented in runtime/doc/options.txt.

Jeremy W. Sherman
You can even automatically base the value of 'colorcolumn' on 'textwidth' with something like :set cc=+1
graywh
That. Is. Awesome. :)
Adam Backstrom
A: 

Well, looking at the :help columns, it's not really being made to mess with.

In console, it's usually determined by console setting (i.e. it's detected automatically) ; in GUI, it determines (and is determined by) the width of the gvim windows.

So normally you just let consoles and window managers doing their jobs by commented out the set columns

I am not sure what you mean by "see and anticipate line overflow". If you want EOL to be inserted roughly column 80, use either set textwidth or set wrapmargin; if you just want soft wrap (i.e. line is wrapped, but no actual EOL), then play with set linebreak and set showbreak.

Ding-Yi Chen