STenyaK
STenyaK

Reputation: 1097

modify vim highlight priorities

I have the four following types of highlighting in my .vimrc (each one displays different colors):

The priority of highlighting seems to be exactly as I listed above. E.g. incremental search coloring will override any of the other matches colors if present in the same character.

I'd like to make hlsearch second in priority, so that it overrides both match and 2match colors (if present in the same character).

Is there any way to accomplish that?

For reference, these are the relevant lines in my .vimrc file:

[...]
set hlsearch
set incsearch
[...]
function Matches()
    highlight curword ctermbg=darkgrey cterm=bold gui=bold guibg=darkgrey
    silent! exe printf('match curword /\V\<%s\>/', escape(expand('<cword>'), '/\'))
    highlight eolspace ctermbg=red guibg=red
    2match eolspace /\s\+$/
endfunction
au CursorMoved * exe 'call Matches()'
[...]

Upvotes: 6

Views: 1135

Answers (1)

Ingo Karkat
Ingo Karkat

Reputation: 172570

The priority of everything you use is fixed; the only way to specify a priority is via matchadd(), which you can use as a replacement for :match and :2match. As the priority of hlsearch is zero, you need to pass a negative priority, e.g. -1).

For example, replace

:match Match /\<\w\{5}\>/

with

if exists('w:lastmatch')
    call matchdelete(w:lastmatch)
endif
let w:lastmatch = call matchadd('Match', '\<\w\{5}\>', -1)

Upvotes: 10

Related Questions