Showing a different background colour in Vim past 80 characters

Vim

Vim Problem Overview


I have seen https://stackoverflow.com/questions/235439/vim-80-column-layout-concerns, but the answer there highlights only actual content that goes over the 80 character mark. I want to have a 100+ column Vim window open with the leftmost 80 columns using the normal background and anything past that using a slightly different background. The Vim window background should be a different color, not just text that goes over the 80 character point. This would indicate how close I am getting to the 80-char point without having to go over it first.

I don't think this is currently possible, but I thought I'd ask just in case.

Maybe it could be done with a plugin?

TextMate Example of Desired Vim Right Margin Highlighting

By selecting "Highlight right margin" in TextMate's general preferences, you can see an example of the desired Vim behavior.

TextMate Right Margin Highlighted Example

Vim Solutions


Solution 1 - Vim

If you have Vim >= v7.3, you can simply add this to your .vimrc to highlight 81 and onward (so 80 is your last valid column):

let &colorcolumn=join(range(81,999),",")

If you don't see a highlight, you may not have a ColorColumn highlight color set. Add this (adjust to suit your preferences):

highlight ColorColumn ctermbg=235 guibg=#2c2d27

Now I like to highlight column 80 as well as 120 and onward, so I have separate "warning" and "danger" markers. You can do that thusly:

let &colorcolumn="80,".join(range(120,999),",")

Example

Here's a screenshot of GVim editing my .vimrc.

GVim editing my .vimrc

I use Ubuntu Mono 11 font and the molokai color scheme. You can find my vim config and all of my dotfiles on GitHub.

Solution 2 - Vim

Highlight 81 column

:set textwidth=80
:set colorcolumn=+1

You may also make some styling:

:hi ColorColumn guibg=#2d2d2d ctermbg=246

Solution 3 - Vim

Solution proposed in comment above. Highlight your background first then highlight the ColorColumn black as an overlay. Set your colorcolumn to lines 1-80.

hi Normal guibg=#32322f ctermbg=236
hi NonText guibg=#32322f ctermbg=236
hi ColorColumn guibg=#000000 ctermbg=0
let &colorcolumn=join(range(1,80),",")

Solution 4 - Vim

try:

:/\%>80v./+

it will mark +80 characters as error

Solution 5 - Vim

I don't think that it's possible to have what you want, but I following this question since I am interested in such a thing myself.

Maybe, I am stating the obvious, but you could setup the highligth in the 70th+ columns to get an indication of how close you get to the 80th column.

Solution 6 - Vim

For those using onedark.vim like me that wants a solution posted by Conner, I added these lines to my vimrc file

let &colorcolumn=join(range(1,80),",")
let g:onedark_color_overrides = {
  \ "black": {"gui": "#2C323C", "cterm": "236", "cterm16": "8"},
  \ "cursor_grey": {"gui": "#282C34", "cterm": "235", "cterm16": "0"}
  \ }

I just swapped the two values, took it from the source code of onedark

Solution 7 - Vim

Note that these "hacks" except for the @Andrew's answer (which is actually official :help colorcolumn) might result in a display bug like this:

enter image description here

It is horrible... And it took me half a day to figure out what part of the configuration was causing this.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDougView Question on Stackoverflow
Solution 1 - VimJustin ForceView Answer on Stackoverflow
Solution 2 - VimAndrewView Answer on Stackoverflow
Solution 3 - VimConnerView Answer on Stackoverflow
Solution 4 - VimGoTTimwView Answer on Stackoverflow
Solution 5 - VimRené NyffeneggerView Answer on Stackoverflow
Solution 6 - VimyujinyuzView Answer on Stackoverflow
Solution 7 - Vim71GAView Answer on Stackoverflow