I have the four following types of highlighting in my .vimrc
(each one displays different colors):
/
search matches)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()'
[...]
After opening login.sh file in vim editor, press ESC key and type ':syntax on' to enable syntax highlighting. The file will look like the following image if syntax highlighting is on. Press ESC key and type, “syntax off” to disable syntax highlighting.
Vim's hlsearch option is a commonly-used way to enable visual feedback when searching for patterns in a Vim buffer. When highlighting of search matches is enabled (via :set hlsearch), Vim will add a colored background to all text matching the current search.
When I search for something, search terms get highlighted. However many times I want to get rid of the highlight, so I do :set nohlsearch . In this way I get rid of highlights for the time being.
Syntax highlighting enables Vim to show parts of the text in another font or color. Those parts can be specific keywords or text matching a pattern. Vim doesn't parse the whole file (to keep it fast), so the highlighting has its limitations.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With