JuanPablo
JuanPablo

Reputation: 24764

vim highlight remove overwrite others hi

I my ~/.vimrc I use this syn for long lines

augroup longLines                                                                                   
     au!
     au! filetype zsh,sh,python,vim,c,cpp
         \ syn match ColorColumn /\%>80v.\+/ containedin=ALL
 augroup END 

but this overwrite other syn, with

ss

without

ss

Why the synoverwrite other highlight?
this is notorious in the last lines

 sys.exit(1)
 import settings

have different colors, with syn, the lines lost normal highlight

Upvotes: 3

Views: 458

Answers (2)

RubyDust
RubyDust

Reputation: 1

I realize you asked this quite some time ago, but in case if other people ask too, perhaps you could try using the matchadd() function, instead, like this:

hi def longLine gui=reverse    "or guibg=pink, or whatever you prefer
augroup longLines
    au!
    au! filetype zsh,sh,python,vim,c,cpp
                \ call matchadd("longLine", "\\%>80v", 0, 9999)
augroup END

Most importantly, make sure that you do NOT set the guifg of whatever highlight group you decide to use. That would overwrite your syntax highlighting.

Another important part (for me, at least) is to use matchadd with 0 as the third parameter so that your Search highlighting remains effective and doesn't get overtaken by the longLine highlighting.

The fourth parameter can be omitted. It's just a constant so that you can :call matchdelete(9999) to easily remove the highlighting again later, if you want.

See :h matchadd and :h matchdelete

Upvotes: 0

ZyX
ZyX

Reputation: 53614

I use the following code:

highlight TooLongLine term=reverse ctermfg=Yellow ctermbg=Red
autocmd BufEnter,WinEnter * if &tw && !exists('b:DO_NOT_2MATCH') |
            \                 execute '2match TooLongLine /\S\%>'.(&tw+1).'v/' |
            \               endif
autocmd BufLeave,WinLeave * 2match

command -nargs=0 -bar Dm let b:DO_NOT_2MATCH=1 | 2match
command -nargs=0 -bar Sm execute '2match TooLongLine /\S\%>'.(&tw+1).'v/' |
            \            silent! unlet b:DO_NOT_2MATCH

If you don’t want to be able to remove this highlighting, depend on textwidth and insist on highlighting spaces that go beyond the limit, then you can truncate this to just

2match TooLongLine /.\%>80v/

This solution uses match-highlight that does not scrap syntax highlighting, but always overrides it.

Upvotes: 2

Related Questions