The code standard that I use indicates that lines should have less than 80 chars. I want to indicate this automatically while I write to avoid raising the error later.

What is a good way of showing it?

  • 5
    @JoshPetrie Not a duplicate, in my opinion. This question is more general because there are other ways to indicate the lines are too long. (See the gitcommit syntax, for example, which uses syntax highlighting.) – 200_success Feb 9 '15 at 22:12

If you have Vim 7.3 or later, you can set colorcolumn, or cc.

:set colorcolumn=80

From :help colorcolumn:

'colorcolumn' is a comma separated list of screen columns that are highlighted with ColorColumn hl-ColorColumn. Useful to align text.

You can also set it relative to your textwidth variable, which is maybe more useful than setting it to an absolute value:

:set colorcolumn=+1

Or set multiple columns:

:set colorcolumn=-1,+30

To change the color of the colorcolumn use :highlight ColorColumn, for example:

:highlight ColorColumn ctermbg=lightgrey guibg=lightgrey

In addition to the colorcolumn option, you can tell vim to automatically push you down to the next line by putting:

set textwidth=80

into your .vimrc file.

From the docs (:h textwidth)

Maximum width of text that is being inserted. A longer line will be broken after white space to get this width. A zero value disables this. 'textwidth' is set to 0 when the 'paste' option is set. When 'textwidth' is zero, 'wrapmargin' may be used. See also 'formatoptions' and ins-textwidth.

When 'formatexpr' is set it will be used to break the line.

NOTE: This option is set to 0 when 'compatible' is set.


Instead of showing a line showing the maximum size, the extra characters can also been highlighted through

:let w:m1=matchadd('ErrorMsg', '\%>80v.\+', -1)

This will set the extra characters to be marked as error (normally, red background, but it depends on the color scheme).

This can be added to .vimrc to do it automatically on each file

:au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)

  • Where can I find documentation for the w:m1 and w:m2 variables? – Kvass Oct 24 '18 at 0:23

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