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.

| improve this answer | |
  • 13
    You can even have multiple comma-separated colorcolumns! – 200_success Feb 4 '15 at 22:02
  • 5
    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 '19 at 15:39
  • In addition to ctermbg to change the background color, one can also set ctermfg to also change the foreground color for that column. – danieldeveloper001 Dec 19 '19 at 1:49
  • 1
    @zyy You'd want to look into the use of autocmd with FileType in your vimrc. I do this with ReStructuredText files with this in my vimrc. (I have the riv plugin setup to identify the file type.) autocmd FileType rst set colorcolumn=100 – Josiah Mar 3 at 14:32

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.

| improve this answer | |

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.

| improve this answer | |
  • 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

I mostly do coding in vim (version 7.4) and I have added the below two lines in my .vimrc to maintain a "colorcolumn" for 100 character length.

:set colorcolumn=100                                                                                
:hi ColorColumn ctermbg=lightcyan guibg=blue

My environment is a color-xterm so I usually populate the "ctermbg" argument.

For someone curious, to find the available colour names in your vim, open vim and run the below command. It will show you a list of colour names that can be used as argument values.

:so $VIMRUNTIME/syntax/colortest.vim
| improve this answer | |

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.