# How can I set up a ruler at a specific column?

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.

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


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.

• You can even have multiple comma-separated colorcolumns! Feb 4, 2015 at 22:02
• 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), ',') Feb 11, 2015 at 11:11
• Is there a way to set this colorcolumn only for specific file types, say Fortran 77 files?
– zyy
Nov 29, 2019 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.
– user26171
Dec 19, 2019 at 1:49
• @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 Mar 3, 2020 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.

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.

• 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) Feb 10, 2015 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


Adding to all the colorcolumn answers, my mappings to make \ln set the highlight line to the current cursor position, and \nl to unset.

function! VerticalLineHere(doeet)
if a:doeet
let &colorcolumn=col('.')
else
let &colorcolumn=0
endif
endfunction

The colorcolumn accepts a comma-separated list of arguments, so in my case I have the following setting in my ~/.vimrc:
set colorcolumn=72,80,120