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.

About these ads
Posted in Vim. 5 Comments »

5 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/ […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 30 other followers

%d bloggers like this: