How to setup a line length marker in vim/gvim?

Some GUI text editors have a vertical line which serves as line length marker (it helps keeping lines shorter than 80 chars in source code files).

Is is possible to have something similar in vim/gvim? I know about ruler vim option, but it is not very handy to follow it visually on a big screen.

Edit: when googling for "colorcolumn" to learn more, I have found that this question is a duplicate of https://stackoverflow.com/questions/235439/vim-80-column-layout-concerns


Solution 1:

Just execute this

:set colorcolumn=72

You can also prefix the argument with - or + to put the marker that many columns to the left or right of textwidth, and it accepts a comma-separated list of columns. I think the colorcolumn option is only in Vim 7.3. See

:help colorcolumn

Solution 2:

From Damian Conway's "More Instantly Better Vim" talk at OSCON 2013:

highlight ColorColumn ctermbg=magenta
call matchadd('ColorColumn', '\%81v', 100)

This results in the character being highlighted in magenta (the screenshot is in DarkCyan) when the line goes over the 80-character maximum.

vim with highlighting enabled

For gVim: it's best to move those 2 lines to the last part of your .vimrc file to ensure it works.