142

I have seen 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

1

6 Answers 6

230

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.

8
  • 3
    colorcolumn is limited to 256 definitions, per :help colorcolumn... in addition, my setting seems to be truncated at some length when using your join technique, although I can't be assed to search through vim's source to figure out what that length is. Also, vim parses the value of this setting as a string every time it enters a window, initializes a buffer, etc, so this is kind of an expensive way of achieving the desired result. Mar 8, 2013 at 4:08
  • 1
    Thanks for posting this, was exactly what I was looking for! Just one more question, is there a way to not set this rules to a specific type of file (.md)?
    – David Dias
    Sep 7, 2015 at 16:05
  • 1
    @DavidDias Sure! You can do something like autocmd FileType markdown let &colorcolumn="" to unset it completely. Sep 8, 2015 at 18:10
  • 1
    updated link to vim config Sep 29, 2016 at 3:32
  • 1
    @obfk don't touch, if you can avoid it.
    – user3451822
    Oct 25, 2017 at 12:03
81

Highlight 81 column

:set textwidth=80
:set colorcolumn=+1

You may also make some styling:

:hi ColorColumn guibg=#2d2d2d ctermbg=246
6
  • 2
    This, and it's also possible to have it highlight up to 256 columns, but unfortunately it takes a list of columns to highlight and there's no shorthand for it, so you would have do to something like :set colorcolumn=+1,+2,+3,+4,+5,+6 etcetera possibly all the way up to +256 for however wide you think you'd like your right margin. This however will only highlight lines which exist in the buffer, not all lines in the window.
    – mkomitee
    Aug 11, 2011 at 11:10
  • 3
    @Dyslexic Tangent: Why not highlight the first 80 columns instead?
    – brice
    Jan 10, 2012 at 10:26
  • 1
    I hadn't thought of that. Set your background to the color you want to use to highlight >80 columns, and set colorcolumn for all columns from 1-80, and set the highlight group for colorcolumn to be the color you want your normal background to be.
    – mkomitee
    Jan 11, 2012 at 13:20
  • 2
    One question, while I can set the colour properly by typing :hi colorcolumn ... it doesn't seem to work when I put it in my .vimrc. Do you know know how to do that?
    – robbrit
    Apr 4, 2012 at 16:18
  • I'm seeing the same issue as robbrit - If I run the highlight command in ex mode, it looks good, but if I have it in my vimrc file, it destroys my colorscheme... Jan 10, 2013 at 20:02
20

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),",")
1
  • 1
    This is simple yet brilliant. Thanks!
    – airstrike
    Feb 17, 2016 at 1:57
9

try:

:/\%>80v./+

it will mark +80 characters as error

3
  • So clever in a pinch. I like it because it adapts to theme changes. Thanks.
    – Benjamin
    Dec 30, 2018 at 19:53
  • @GoTTimw Too bad that this can't be applied in a configuration file...
    – 71GA
    Apr 19, 2022 at 10:10
  • @71GA match Error /\%81v.\+/ thats for older vim in newer i use 'hi ColorColumn guibg=#000000 ctermbg=233 let &colorcolumn="80,".join(range(120,999),",")' to mark single column 80 and then all columns past 120
    – GoTTimw
    May 19, 2022 at 14:12
0

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.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.