Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Most of the text editors/IDEs that I use these days have a feature where it can display a line at a certain character length in the text buffer. It's useful when you want to keep lines of text under a certain length.

Is there any way to get vim to do this (preferably to use the already-defined textwidth value)? It will automatically wrap at that point, but I'd really like to be able to see where it is as well.

In case it matters, I'm mainly using gVim on Windows, but I'd love it if the solution works across vim versions.

share|improve this question
up vote 32 down vote accepted

for (g)vim, use the following:

set colorcolumn=80

or whatever width you wish. Works in both vim & gvim. I have mine within an IF so it's conditional based on which type of files I edit.

You may also use a +x/-x to base position of the column +/- from &textwidth.

set textwidth=80
set colorcolumn=-2

would effective draw the colored bar at char position 78. Of course, you may or may not set textwidth yourself, so it might be 0 (default). I use the absolute position form.

You may also change the color used if you wish:

highlight ColorColumn ctermbg=green guibg=orange

(I don't recommend THOSE colors though)

This option was added in (g)vim 7.3.

share|improve this answer
What version of Vim is this supported in? It's not working for me in gvim 7.2. – Herms Nov 5 '10 at 16:53
I'm running 7.3. I just pulled the source code and looked, this command was introduced in 7.3. – lornix Nov 5 '10 at 17:18
Works for me on Ubuntu 12.04 with vim 7.3. gvim 7.3 for windows has been available since at least October of 2010. @Herms can you make this the answer? – poindexter Jan 3 '13 at 15:40
It would be nice if this worked when using multiple windows side-by-side. My monitor is wide enough to show several windows in a row, each one > 80 chars but colorColumn only works on the first (leftmost) window. – Eno Mar 16 '15 at 16:28

Per a StackOverflow answer:

highlight OverLength ctermbg=red ctermfg=white guibg=#592929
match OverLength /\%81v.*/

Adjust to suit your taste.

share|improve this answer
Hm, not quite what I'm looking for (only displays when characters go past the line), but it's better than nothing. – Herms Aug 13 '09 at 20:05

There's a snippet at Google Code you can try:

augroup vimrc_autocmds
    autocmd BufRead * highlight OverLength ctermbg=red ctermfg=white guibg=#592929 
    autocmd BufRead * match OverLength /\%81v.*/
augroup END
share|improve this answer
Hmm, that thread suggests that windows' gvim already supports it, but I can't figure out how to turn it on. – Herms Aug 13 '09 at 19:41
There is some code to add to your gvimrc file futher down in the thread. – EBGreen Aug 13 '09 at 19:46
seems like that just changes the background for characters that go beyond that line. I'd rather have an always-visible guide like other editors have. – Herms Aug 13 '09 at 20:11
Due to the nature of the gui I'm not positive that is currently possible. – EBGreen Aug 13 '09 at 20:14

This was often discussed on #vim, and in some forums. As far as things stand now, it is not possible. So the mentioned solution is your only option, afaik.

The thing is, vim can do anything with places where there are characters (them being letters, numbers or just plain whitespace). But it cannot paint a background in a different colour (like you would like), if there is nothing there. And before you type something, there isn't anything there, so it cannot draw a line/margin.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .