15

In vim I am trying to have it highlight my screen when it goes over 80 characters (I am a kernel developer and this is a requirement for kernel code). I am using the following in my .vimrc file to do this

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

which was suggested here: Vim 80 column layout concerns

However, I am colorblind and as a result I cannot use the default colorscheme and see the text on the screen so I change my default colorscheme using :colorscheme torte as well. However, it appears that the colorscheme overrides the highlighting on me (it highlights fine when I don't have the colorscheme specified, and doesn't when it is specified).

Does anyone have any ideas on how to make this work with the colorscheme change? Thanks for your help,

Community
  • 1
  • 1
SaxyTimmy
  • 213
  • 2
  • 9

3 Answers3

27

If you have Vim 7.3+ you can use:

:set colorcolumn=81
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101
  • That's really nice, never knew about it. I always created highlight matches, this is way better for working with aligned text. +1, and it will be a matter of preference between this answer and mine. This one is quite easy to set, however only one column gets highlighted (nothing that a `:set cc=80,81,82,83,84,85` doesn't solve :-). – sidyll Oct 17 '11 at 18:12
  • Yeah... I saw that solution earlier, but I have 7.2 and I am not supposed to upgrade it at work yet. Thanks for your help! – SaxyTimmy Oct 17 '11 at 18:52
  • @sidyll you can also use numbers relative to the 'textwidth'. `set colorcolumn=+1,+2,+3`. See `:h 'cc'` for more information. – Peter Rincker Nov 21 '13 at 18:22
20

That's because most color schemes have this line in their beginning:

hi clear

This clear all highlighting groups created (you created OverLength). This is good, because no colorscheme has every thing set, and colors from the previous scheme used could remain appearing.

To solve, instead of creating a new group, match this pattern as Error which is a default Vim group, will be present in every color scheme. Change your two lines by one simply:

match Error /\%81v.\+/
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • @sidyll, The highlighting somehow disappears when creating a split window. – vrybas Jul 06 '13 at 11:13
  • @vrybas I'm not sure if I understand it. Is it a vertical split? Please consider posting a separate question ;-) – sidyll Jul 06 '13 at 14:27
0

You can use Colorscheme event to define OverLength group:

augroup AddOverLength
    autocmd!
    autocmd ColorScheme * :highlight OverLength ctermbg=red ctermfg=white guibg=#59292
augroup END

Note that you must either put all calls to :colorscheme that happen in your vimrc after this definition or explicitely launch this event by using :doautocmd AddOverLength ColorScheme.

ZyX
  • 52,536
  • 7
  • 114
  • 135