422

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

SK98
  • 29
  • 3
cdleary
  • 63,281
  • 49
  • 155
  • 190
  • 61
    It's considered good code style in many circles; for example, Python's Style Guide (PEP8) recommends 79 character lines: http://www.python.org/dev/peps/pep-0008/ – cdleary Oct 24 '08 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. – Matthew Scharley Oct 24 '08 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 '09 at 13:47
  • 25
    @davr it's incredibly useful for editing several files side-by-side. – aehlke Sep 13 '10 at 09: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... – Mark K Cowan Nov 10 '14 at 12:52

14 Answers14

779

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.

Jeremy W. Sherman
  • 34,925
  • 5
  • 73
  • 108
  • 27
    You can even automatically base the value of 'colorcolumn' on 'textwidth' with something like :set cc=+1 – graywh Oct 01 '10 at 16:37
  • 1
    has("colorcolumn") didn't work for me. I had to use exists("&colorcolumn") instead. – SpoonMeiser Nov 24 '10 at 14:19
  • 5
    Very nice, except that vim < 7.1.40 does't support matchadd. – Greg Jandl Dec 15 '11 at 16:20
  • I found the `au BufWinEnter ...` better than the `colorcolumn` option. The only downside is that it also highlights lines in quickfix window, which is a little bit annoying. – mMontu Dec 30 '11 at 17:15
  • 19
    you can also change the color of the column with `highlight ColorColumn ctermbg=7` – null_radix Jan 08 '13 at 03:48
  • 2
    Documentation for this option is at http://vimdoc.sourceforge.net/htmldoc/options.html#%27colorcolumn%27 – JohnTESlade Apr 23 '14 at 13:50
  • I'd upvote this but I wouldn't like to ruin the 666 note of the post. – Jérôme Mar 20 '17 at 15:22
  • I can't seem to be able to control the background color, using `highlight ColorColumn ctermbg=7` for example, when color schemes like `256-jungle` or `vividchalk` are set. I guess the latter ones override this setting. How can I "fix" this? – Nikos Alexandris Jul 24 '18 at 12:05
  • You edit the theme or, as I would do, place this command after where you set the colorscheme in `vimrc`, so it overrides the theme rather than the other way around. – miyalys Nov 06 '18 at 10:49
592

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
  • 8,843
  • 4
  • 25
  • 21
  • 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 '08 at 08:27
  • 10
    I think a subtle correction is /\%81v.\+/ -- for some reason the .* highlights places where characters do not exist. – cdleary Oct 29 '08 at 01:05
  • Does this method account for tabs = 8 spaces? or does it count tab as one character? – user17925 Jan 01 '10 at 05:50
  • @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 Jan 06 '10 at 15:19
  • 14
    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 01:47
  • 1
    Didn't display anything at all until after I applied @cdleary's fix, now works beautifully. – meager Mar 30 '10 at 16:53
  • If your terminal supports it, you can 'set t_Co=256'. Which gives you 256 colors to play with. So you can also achieve a more subtle background. – exhuma Jul 18 '11 at 08:58
  • I like this; 80 characters is good, but you can sacrifice this for readability and style. It should be a soft limit, with 100 as a hard limit. Actually, this doesn't seem to work for me. I need to do some playing around. Thanks for the start though. – nlucaroni Jan 20 '12 at 16:26
  • when I use this in a long string between `"`, like `"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"a` only show the highlight in the last `a` http://cl.ly/I9VY – JuanPablo Jul 17 '12 at 18:30
  • Can anyone explain the regex? I'm curious why the `\%80v.*` highlights endline characters but not `\%80.\+` – Eric Hu Oct 03 '12 at 00:45
  • 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 '13 at 16:27
  • 3
    Check out http://stackoverflow.com/a/10993757/9822 if this solution is only working in your first buffer. – Jesse Vogt Sep 06 '13 at 20:52
  • 1
    FYI: setting the `ctermbg` is a little dicey, because it interferes with things like colorscheme-based syntax highlighting as well as the highlighting of search results. If this doesn't cause issues for you, then go to! – pieman72 Jun 04 '14 at 08:52
  • Is there any way we can enforce this limit only to .c files. – Ravi Chandra Sep 10 '14 at 11:50
  • I can temporarily disable it using `:match OverLength /\%1000v.\+/`. Is there a better way? – Alex Harvey Mar 13 '18 at 14:37
  • @SimonHoward : I've been using your command for a while, but it seems to not work when using split windows using ` + v` in vim. Would be great if it could work in all setups. As soon as close all the splits and reopen any file, I can then see the red highlight. – aspiring1 Oct 23 '19 at 09:25
57

Shorter way:

match ErrorMsg '\%>80v.\+'
Gaelan
  • 1,081
  • 10
  • 25
Maksim Vi.
  • 8,589
  • 12
  • 54
  • 83
  • 9
    I really like this because it doesn't pollute my buffer with a long vertical line! (Y) – Ionică Bizău Jun 11 '14 at 08: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 '18 at 07:10
53

I prefer:

highlight ColorColumn ctermbg=gray
set colorcolumn=80
CDub
  • 12,780
  • 4
  • 47
  • 65
wieczorek1990
  • 5,224
  • 1
  • 22
  • 17
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.

the Tin Man
  • 150,910
  • 39
  • 198
  • 279
Z.Zen
  • 838
  • 1
  • 10
  • 19
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.

Dominykas Mostauskis
  • 6,521
  • 2
  • 42
  • 58
  • Why did you add a `FileType scala,java`? (not working for me) – Dorian Feb 25 '14 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. – Dominykas Mostauskis Feb 25 '14 at 21:45
  • What font are you using in the screenshot? I find it very pleasing to the eye. – Rook Apr 19 '14 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 :) – Mark K Cowan Nov 10 '14 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 '16 at 13:42
  • @Mnebuerquo I'm not using width markers anymore, so can't help you. Nowadays my terminal windows are simply 80 chars wide, which is default anyway. I was overthinking it in the past. – Dominykas Mostauskis Mar 21 '16 at 23:06
  • @DominykasMostauskis: Thanks. I'm moving towards using 80 char width terminals too. I'm using multiple splits in tmux. I think that's just the easiest. I'm using set colorcolumn=80 now to get a vertical line hint. I definitely didn't like highlighting all characters past 80. It's mostly a pain to edit legacy code where the original author didn't respect any line length limits. – Mnebuerquo Mar 22 '16 at 01:03
  • I feel I should mention that some time has passed, and I've long stopped using 80+ width terminal windows. Above is something I used for a little while, but now I just keep my terminal windows at 80. – Dominykas Mostauskis Oct 30 '17 at 13:45
  • I feel I should mention that some time has passed, and I've long stopped using 80+ width terminal windows. Above is something I used for a little while, but now I just keep my terminal windows at 80. – Dominykas Mostauskis Oct 30 '17 at 13:45
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.

Shanded
  • 188
  • 1
  • 5
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

0x8BADF00D
  • 5,725
  • 2
  • 36
  • 34
7

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
  • 115,776
  • 51
  • 189
  • 215
5

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
  • 14,753
  • 2
  • 42
  • 45
  • 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
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).

Aristotle Pagaltzis
  • 101,052
  • 21
  • 94
  • 96
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.

Ding-Yi Chen
  • 2,006
  • 26
  • 26
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> 
ErichBSchulz
  • 12,945
  • 5
  • 47
  • 50
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.

Mike L
  • 19
  • 1