Take the 2-minute tour ×

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.?

share|improve this question
It's considered good code style in many circles; for example, Python's Style Guide (PEP8) recommends 79 character lines: python.org/dev/peps/pep-0008 –  cdleary Oct 24 '08 at 22:27
It's not an absolute "you must have 80 character lines", but it does make it nicer to read on some systems, like for instance any console based application that not running inside a GUI console window that can be resized. –  Matthew Scharley Oct 24 '08 at 23:25
@davr: First, it's easier to read. Some studies suggest a short line length (76 characters a line) is most agreeable and fastest to be read online. Do you know this: suddenly your eyes are on the wrong line when reading? This rarely happens with 80 characters. Secondly I often edit or diff visually with two files side-by-side on my 1600 pixel wide monitor. This only works well if the file does not have lines more than 80 characters. So I often reformat long lines before I can work efficiently. –  nalply Dec 19 '09 at 13:47
@davr it's incredibly useful for editing several files side-by-side. –  aehlke Sep 13 '10 at 9:33
add comment

12 Answers

up vote 340 down vote accepted

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).

share|improve this answer
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. Oct 25 '08 at 8:27
I think a subtle correction is /\%81v.\+/ -- for some reason the .* highlights places where characters do not exist. –  cdleary Oct 29 '08 at 1:05
This made my day. –  Casey Sep 9 '09 at 20:38
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 Mar 17 '10 at 1:47
For some reason it only works in the first opened buffer for me (first window of the first tab) in my vim... –  lajarre Jul 25 '13 at 16:27
show 14 more comments

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 exists('+colorcolumn')
  set colorcolumn=80

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.

share|improve this answer
You can even automatically base the value of 'colorcolumn' on 'textwidth' with something like :set cc=+1 –  graywh Oct 1 '10 at 16:37
That. Is. Awesome. :) –  Adam Backstrom Oct 26 '10 at 19:22
Actually 'colorcolumn' is option, not a feature, so use exists(). if exists('+colorcolumn') –  Valery Viktorovsky Feb 4 '11 at 21:24
The OP should really move the accepted answer to this answer... –  VxJasonxV Mar 24 '11 at 16:42
you can also change the color of the column with highlight ColorColumn ctermbg=7 –  null_radix Jan 8 '13 at 3:48
show 5 more comments

Shorter way:

match ErrorMsg '\%>80v.\+'
share|improve this answer
I really like this because it doesn't pollute my buffer with a long vertical line! (Y) –  Ionicã Bizãu Jun 11 at 8:53
add comment

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!

share|improve this answer
add comment

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

share|improve this answer
Link only answers aren't really considered helpful on our site. Would be nice if you could expand this answer and explain the basics? –  Kev Aug 30 '12 at 23:25
add comment

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).

share|improve this answer
add comment

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).

share|improve this answer
add comment

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.

share|improve this answer
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 Oct 24 '08 at 22:51
add comment

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.

share|improve this answer
add comment

You can try this to set the window size to allow 80 characters of actual text. This still doesn't work with vertical splits though.

let &co=80 + &foldcolumn + (&number || &relativenumber ? &numberwidth : 0)

This requires vim 7+, 7.3 for relativenumber.

share|improve this answer
add comment

enter image description here

Minimalistic, not-over-the-top approach. Only the 79th character of lines that are too long gets highlighted. It overcomes a few common problems: works on new windows, overflowing words are highlighted properly.

augroup collumnLimit
  autocmd BufEnter,WinEnter,FileType scala,java
        \ highlight CollumnLimit ctermbg=DarkGrey guibg=DarkGrey
  let collumnLimit = 79 " feel free to customize
  let pattern =
        \ '\%<' . (collumnLimit+1) . 'v.\%>' . collumnLimit . 'v'
  autocmd BufEnter,WinEnter,FileType scala,java
        \ let w:m1=matchadd('CollumnLimit', pattern, -1)
augroup END

Note: notice the FileType scala,java this limits this to Scala and Java source files. You'll probably want to customize this. If you were to omit it, it would work on all file types.

share|improve this answer
Why did you add a FileType scala,java? (not working for me) –  Dorian Feb 25 at 14:39
@Dorian it limits this to Scala and Java filetypes, because those were the filetypes I was working on at the time. I neglected to mention that. Updating answer. –  Dominykas Mostauskis Feb 25 at 21:45
What font are you using in the screenshot? I find it very pleasing to the eye. –  ldigas Apr 19 at 21:45
add comment

this one is out of left field but its a nice little map for resizing your current split to 80 characters if you've got the line numbers on:

" make window 80 + some for numbers wide  
noremap <Leader>w :let @w=float2nr(log10(line("$")))+82\|:vertical resize <c-r>w<cr> 
share|improve this answer
add comment

Your Answer


