430

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

This has some unfortunate side effects:

  1. 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.
  2. I also start new (g)Vim sessions instead of splitting windows vertically. This is because vsplit forces me to 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.?

5
  • 61
    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, 2008 at 22:27
  • 4
    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. Oct 24, 2008 at 23:25
  • 47
    @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, 2009 at 13:47
  • 25
    @davr it's incredibly useful for editing several files side-by-side.
    – aehlke
    Sep 13, 2010 at 9:33
  • 2
    I make far less bugs in both C/C++ and JS code since I adopted an 80 char line width (from the kernel coding style). I occasionally overflow it by a couple, but rarely... Nov 10, 2014 at 12:52

16 Answers 16

790

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
else
  au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
endif

See also the online documentation on the colorcolumn option.

9
  • 29
    You can even automatically base the value of 'colorcolumn' on 'textwidth' with something like :set cc=+1
    – graywh
    Oct 1, 2010 at 16:37
  • 1
    has("colorcolumn") didn't work for me. I had to use exists("&colorcolumn") instead. Nov 24, 2010 at 14:19
  • 5
    Very nice, except that vim < 7.1.40 does't support matchadd.
    – Greg Jandl
    Dec 15, 2011 at 16:20
  • 20
    you can also change the color of the column with highlight ColorColumn ctermbg=7
    – null_radix
    Jan 8, 2013 at 3:48
  • 2
    Documentation for this option is at vimdoc.sourceforge.net/htmldoc/options.html#%27colorcolumn%27
    – John Slade
    Apr 23, 2014 at 13:50
594

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

16
  • 3
    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, 2008 at 8:27
  • 10
    I think a subtle correction is /\%81v.\+/ -- for some reason the .* highlights places where characters do not exist.
    – cdleary
    Oct 29, 2008 at 1:05
  • 15
    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. Mar 17, 2010 at 1:47
  • 8
    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, 2013 at 16:27
  • 3
    Check out stackoverflow.com/a/10993757/9822 if this solution is only working in your first buffer.
    – Jesse Vogt
    Sep 6, 2013 at 20:52
58

Shorter way:

match ErrorMsg '\%>80v.\+'
3
  • 9
    I really like this because it doesn't pollute my buffer with a long vertical line! (Y) Jun 11, 2014 at 8:53
  • 1
    ErrorMsg is a highlight-group. There are a lot of default highlight groups in Vim which you can use based on the color encoding you want for this case.
    – crisron
    Apr 27, 2018 at 7:10
  • didnt work for me in 8.2
    – sjas
    Mar 15 at 10:58
54

I prefer:

highlight ColorColumn ctermbg=gray
set colorcolumn=80
1
  • 2
    I like this one. I used black instead of gray and it was more subtle since my background is almost black but not quite. May 29, 2018 at 23:37
30

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 exceeds column 80.

1
  • 2
    Might want to add guifg=white on the end there. May 23, 2017 at 2:09
18

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

9
  • Why did you add a FileType scala,java? (not working for me)
    – Dorian
    Feb 25, 2014 at 14:39
  • 2
    @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. Feb 25, 2014 at 21:45
  • What font are you using in the screenshot? I find it very pleasing to the eye.
    – Rook
    Apr 19, 2014 at 21:45
  • 1
    @Idigas: Agreed, and I feel like maybe Comic Sans may have been a bad choice when I originally set up PuTTY :) Nov 10, 2014 at 12:54
  • I wasn't able to get the snippet in this answer to work in Vim 7.4. (I removed the FileType filter too.)
    – Mnebuerquo
    Mar 21, 2016 at 13:42
15

A nice way of marking just the first character going out of the specified bounds:

highlight ColorColumn ctermbg=magenta "set to whatever you like
call matchadd('ColorColumn', '\%81v', 100) "set column nr

From Damian Conway's talk.

14

You also can draw line to see 80 limit:

let &colorcolumn=join(range(81,999),",")
let &colorcolumn="80,".join(range(400,999),",")

Result:

enter image description here

0
8

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

6

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.

1
  • 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, 2008 at 22:51
5

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

3

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.

2

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> 
1

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.

1

vim 8.2

match ErrorMsg '\%80v.\+'
0

set column line using set colorcolumn

set autoindent                                                                  
set smartindent                                                                 
set nowrap                                                                      
set hlsearch                                                                    
set tabstop=4                                                                   
set shiftwidth=4                                                                
syntax on                                                                       
set colorcolumn=80

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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