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

80

35

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

vtest

Posted 2011-02-24T13:44:23.420

Reputation: 4 424

duplicate on another site... http://stackoverflow.com/questions/235439/vim-80-column-layout-concerns

– Trevor Boyd Smith – 2016-10-21T14:04:42.807

Answers

138

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

garyjohn

Posted 2011-02-24T13:44:23.420

Reputation: 29 085

10Wow, vim never ceases to amaze me! – Peter Nore – 2011-06-21T02:38:38.197

5Nice! Also useful when columns=80, wrap is set and colorcolumn=81,161,241,321,401,481,561,641,721,801 – shows up to 10 wrapped lines with the first column highlighted. – Amir – 2012-06-11T15:45:40.953

21

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.

Isxek

Posted 2011-02-24T13:44:23.420

Reputation: 3 785

Huh! I just answered your very similar question about this... small world! http://superuser.com/questions/771558/line-length-highlighting-works-in-console-vim-not-gui

– lornix – 2014-06-21T05:29:46.053

I put this answer in, then remembered I couldn't do it in gVim, so I asked. ;) – Isxek – 2014-06-21T06:18:48.163

Wow, this is really great! This is the kind of feature I wouldn't even know to look for. I'm using the pattern '\$81v\S' which matches only non-whitespace characters. This means the highlight will not show when the line is exactly 80 characters long (which is something that really bothered me). See this for more info http://stackoverflow.com/questions/12985042/vim-search-for-lines-with-or-without-character-in-specific-column

– fvgs – 2016-05-08T08:39:06.080

1Want to make a couple of corrections to my previous comment. The $ should have been a %. Likewise, I now use the pattern \%81v. since this ensures the 81st column will be highlighted for any character in the 81st column, which is probably what you want. – fvgs – 2016-06-20T08:30:53.767

2

You could try this:

grep '.\{81\}' file

or

set colorcolumn=80

(or the shorthand equivalent)

set cc=80

or as aforementioned:

match ErrorMsg '\%>80v.\+'

Gabriel Zalles

Posted 2011-02-24T13:44:23.420

Reputation: 21

0

Below is a clumsy trick from Hacking Vim: A Cookbook to get the Most out of the Latest Vim Editor by Kim Schultz.

It highlights with ErrorMsg (usually bright red) any lines that go over 80 characters. Works well for me.

function! RemoveWidthLimitWarnigns()
    silent! call matchdelete(4)
endfunction
function! InsertWidthLimitWarnings()
    call RemoveWidthLimitWarnigns()
    call matchadd("ErrorMsg", "\\%>79v.\\+", 10, 4)
endfunction

nperson325681

Posted 2011-02-24T13:44:23.420

Reputation: 1 401

What am I supposed to do with this snippet? Just stick it into .vimrc? – vtest – 2011-02-24T16:02:48.287

Trying first by just calling :call matchadd("ErrorMsg", "\\%>79v.\\+", 10, 4) suffices. But I wouldn't recommend this anymore since @garyjohn knew something better – nperson325681 – 2011-02-24T17:40:38.940