85

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?

6 Answers 6

113

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.

5
  • 18
    You can even have multiple comma-separated colorcolumns! Commented Feb 4, 2015 at 22:02
  • 6
    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
    Commented Feb 11, 2015 at 11:11
  • Is there a way to set this colorcolumn only for specific file types, say Fortran 77 files?
    – zyy
    Commented 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
    Commented Dec 19, 2019 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
    Commented Mar 3, 2020 at 14:32
28

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.

0
16

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.

1
  • 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
    Commented Feb 10, 2015 at 9:54
4

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
0

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
nnoremap <leader>line :call VerticalLineHere(1)<cr>
nnoremap <leader>ln :call VerticalLineHere(1)<cr>
nnoremap <leader>noline :call VerticalLineHere(0)<cr>
nnoremap <leader>nl :call VerticalLineHere(0)<cr>
0

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
hi ColorColumn ctermbg=lightgrey guibg=lightgrey

See also:

1
  • It also accepts special values that draw columns relative to textwidth, IIRC
    – D. Ben Knoble
    Commented Nov 9, 2022 at 22:34

Your Answer

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

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