Vim Line Length Marker


Since version 7.3, Vim includes a way to highlight a line length by coloring a specific column of text. Many teams enforce a line length limit as a coding standard and this feature provides a useful visual indicator. Vim’s line length marker is called a “color column” (see :help colorcolumn) . In fact, it can be multiple columns in case you wish to highlight several increments across the width of your text. Here’s how to use Vim’s colorcolumn:

Vim Line Length Marker Options

There are two ways to use the colorcolumn setting: using absolute numbers or relative numbers. In the case of absolute, simply use set colorcolumn=100 where 100 is the column number you wish to highlight. You can just run this command in your present buffer or place that statement in your .vimrc.

In the case of relative numbers, use set colorcolumn=+1 or -1 where 1 is a number relative to the current textwidth value you have set. Use +0 to have the marker directly on your max text width. The advantage of using relative numbers is that if you have different text width settings for different file types, the colorcolumn setting will respect that difference. (See our post about changing Vim settings by file type.).

Multiple Color Columns in Vim

You can actually pass a comma-separated list of values to set colorcolumn and Vim will highlight up to 256 different columns. This can be useful when you want to evenly space text across a page or even show when text is wrapped along a long line.

Vim colorcolumn Demo

In the demo below, I first show how to use multiple color columns to highlight everything after a certain width on my screen. My initial setting is colorcolumn=60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80 Then I show several columns evenly spaced with colorcolumn=20,40,60 and finally I use colorcolumn=+0 to display the line width marker exactly on my textwidth setting, which is 60.

How do I enable a line length marker in Vim?

Use Vim's colorcolumn setting to enable a line length marker in Vim. The value of this setting will enable a visual highlight at that particular line length. Use a relative number like +1 or -1 to have the value offset from your textwidth setting or an absolute number like 80 to show the marker that that specific width.

How can I change the color of the Vim colorcolumn?

You can change the color of Vim colorcolumn using the highlight setting. For example the following will show the color column in red:
highlight ColorColumn ctermbg=1

Can I highlight multiple columns in Vim?

Yes, you can highlight multiple columns in Vim as a way to show evenly spaced text. Just pass multiple, comma-separated valued, to the colorcolumn setting such as: set colorcolumn=10,20,30,40,50,60,70,80.

Can I abbreviate colorcolumn?

Yes, you can abbreviate colorcolumnin your .vimrc as cc. However, since space is not at a premium in your config, we recommend leaving the full name for clarity. The cc short name can be useful when setting a quick line length marker in the current Vim window on a one-off basis.

What are the Vim colorcolumn docs?

colorcolumn is a comma separated list of screen columns that are highlighted with ColorColumn. Useful to align text. Will make screen redrawing slower. The screen column can be an absolute number, or a number preceded with + or -, which is added to or subtracted from textwidth.
:set cc=+1 " highlight column after 'textwidth'
:set cc=+1,+2,+3 " highlight three columns after 'textwidth'
:hi ColorColumn ctermbg=lightgrey guibg=lightgrey
When textwidth is zero then the items with - and + are not used. A maximum of 256 columns are highlighted.

Does colorcolumn work in Neovim?

Yes, colorcolumn works in Neovim and all versions of Vim after 7.3.

How useful was this tip?

Average rating 4.7 / 5. Vote count: 42

No votes so far! Be the first to rate this tip.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Written by

Colin Bartlett

102 Posts

Vim enthusiast and software developer for more than 20 years.
View all posts