When coding in different languages, I tend to need to restrict my lines to a specific length (usually 79 or 80 characters).

I've seen this done in other editors through the use of a ruler (solid line displayed at the column) or by changing the color of the text to indicate that it has gone over.

How can I have Vim indicate to me when I am approaching or going over 79 characters in a single line?


You can display a ruler at a specific line using the :set colorcolumn (:set cc for short) option which is only available in Vim 7.3 or later.

set colorcolumn=80

This will set the background color of that column to red, giving you a visual ruler to work from.

Picture of default colorcolumn set to 80

If you want to use a different color other than red (which really stands out), you can change the color by setting the highlight for ColorColumn.

highlight ColorColumn ctermbg=0 guibg=lightgrey

Customized color for the column

You can set the color for terminal versions of Vim using the cterm argument and GUI versions of Vim using the guibg argument. The 0 is the value of the ASCII escape code for black, which is grey when brightened (which it is, by default). The value lightgrey is used for GUI versions of Vim, like gVim, to change the background color to a light grey.

  • 10
    You can even have multiple comma-separated colorcolumns! – 200_success Feb 4 '15 at 22:02
  • 4
    Combine that with join & range, and you can have a bunch of columns colored with very little code. My .vimrc has: execute "set colorcolumn=" . join(range(81,335), ',') – Cody Poll Feb 11 '15 at 11:11
  • Is there a way to set this colorcolumn only for specific file types, say Fortran 77 files? – zyy Nov 29 at 15:39

As an addendum to Kevin's answer, you can have multiple colorcolumns. When I code, I sometimes have a "soft" limit at 80 columns and a "hard" limit at 120 columns. So I want a line at 80, and then every column after 120 to be colored.

I do this with

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

Of course, this can be easily modified to other preferences.


Vim 7.3 brings the colorcolumn option, as detailed very well in other answers.

However, if you don't have version 7.3 for whatever reason, you can still achieve a visual indication that you are exceeding a particular column count using vim's match functionality (see :help match for details).

Essentially, the match commands allow you to create persistent highlights for text matching a given regular expression. :match ColorColumn "\%80v." will highlight text in column 80 with the "ColorColumn" group. You can of course substitute any highlight group, and any column value. If you want a strong visual indication, the expression "\%>79v.\+" will highlight column 80 and beyond.

(\%80v means "match in virtual column 80," and \%>79v means "match after virtual column 79; see :help /\%c for more.)

This approach will only highlight when there are actual characters present in the specified columns, however, which makes it visually less consistent than colorcolumn.

  • 4
    Personally I find the less consistent approach of matches better, as it highlights only those lines where I am exceeding the 80-column limit, rather than showing a (to me) intrusive big red line on the right of the screen. Hence in my vimrc for Python I use call matchadd('ColorColumn', '\(\%80v\|\%100v\)') (80 and 100 being the suggested lengths in PEP 8) – jalanb Feb 10 '15 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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