Different background color in Vim past 80 columns

Or any number of columns, but 80 seems to be the standard for code.

Preface

I’ve tried different ways of highlighting lines past 80 columns but was never satisfied with the results. Since Vim 7.3 there’s a nice colorcolumn option, but it just didn’t look any good:

Vim with :set colorcolumn=80

It’s not just the red color, which can be easily changed, but the whole “highlight a single seemingly random column” didn’t appeal to me. After searching for inspiration on Google, I found this StackOverflow question with the following screenshot of TextMate:

TextMate with “Highlight right margin” option

Well, well, that looks much prettier. Is it possible to get something similar in Vim?

Highlight range of columns

The answer, as usual in Vim, is “of course!”. From a comment on StackOverflow I learned that you can let colorcolumn highlight several columns:

colorcolumn=81,82,83,84,85

So if we want to highlight the background past 80 columns we would just have to set colorcolumn to all integers from 81 to (81+256-1), since it can highlight a maximum of 256 columns (according to my tests it’s actually 255). Not that hard to do, but there should be a more elegant way to define the range. A Google search proved that my intuition was right; you can add the following in your .vimrc instead:

execute "set colorcolumn=" . join(range(81,335), ',')

If you want to change the colors you can adjust the ColorColumn highlight in your .vimrc or colortheme. Here’s my final result:

Vim with a different background past column 80

A smarter way?

A comment on StackOverflow pointed out that you could highlight the first 80 columns instead and change the background of Normal. I tried to do that, but it clashed with some other highlights, for example my DiffAdd, DiffChange, DiffDelete, and DiffText ctermbg seemed to be overridden. Another limitation I see with this method is that you can’t set a special textcolor for text past 80 columns, if that’s what you fancy. But that’s only as far as I know, and I admit that I know very little when it comes to the inner workings of Vim.

Posted in Vim. 7 Comments »

7 Responses to “Different background color in Vim past 80 columns”

  1. Hans Says:

    Trivia: Funnily enough, this is my 80th post on this blog. Hooray!

  2. Mike Funk Says:

    Thanks for this, I didn’t think you could do this in Vim but I wanted to.

  3. :) Says:

    Hi ChenChen. Nice blog and website u have. guess who?! 😉

  4. Hans Says:

    Why, hello there Ms. Zingyhall. 🙂 Thanks for your nice comment!

  5. 【vim】80桁教信者の憂鬱 | blog.remora.cx Says:

    […] Different background color in Vim past 80 columns | Who Says Penguins Can’t Fly? http://blog.hanschen.org/2012/10/24/different-background-color-in-vim-past-80-columns/ […]

  6. Errin Larsen (@irk3n) Says:

    I have no idea if anyone will ever read this, but I found another way to handle this sort of thing in a slightly more dynamic way. As I found this blog post searching for the solution, I thought someone else might like the solution I found.

    You may not know that if you `set textwidth=80`, that you can then `set colorcolumn=+2` ((note the ‘+’)). This will add a colorcolumn at the column + n (2, in this example) of your textwidth column. In this example, that would be column 82.

    You can also chain multiple colorcolumn values together, separated by commas (as pointed out, above; thank you Hans!), so, for example, `set colorcolumn=+2,+3,+4,+5,+6` would color the 82nd, 83rd, 84th, 85th, and 86th columns (5 columns, starting 2 columns past the textwidth).

    Combining this, with Hans tip above, my vimrc now looks like this:
    `execute “set colorcolumn=” . join(map(range(2,259), ‘”+” . v:val’), ‘,’)

    Breaking that apart:

    `range(2,259)` generates a list of the numbers 2 through 259 (e.g. [2,3,4,…,259]) [see :help range()]

    `map(range(2,259), ‘”+” . v:val’)` will take each number in the list, and add “+” to the front of it. (e.g. [“+2″,”+3″,…”+259″]) [see :help map()]

    `join(map(range(2,259), ‘”+” . v:val’), ‘,’)` will combine all elements of that list into a single string, separated by commas. (e.g. “+2,+3,+4,…,+259”) [see :help join()]

    All together, that accomplishes the same goal as Hans’ tip (above), but has the added benefit of always being RELATIVE to the current textwidth setting!

    Now, when I change the textwidth from 79 (with colorcolumns starting at 81), to 72, the colorcolumns shift as well, and now start at 74. All in all, pretty handy – at least, for me!

  7. soziflip Says:

    Nice, thanks!


Leave a comment