Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify vim highlight priorities

Tags:

vim

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

  • incsearch (highlight-as-you-search matches)
  • match (current word, a la visual studio editor)
  • 2match (trailing spaces at end of line)
  • hlsearch (regular / 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()'
[...]
like image 635
STenyaK Avatar asked Dec 28 '12 11:12

STenyaK


People also ask

How do I set highlights in Vim?

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.

How do I highlight and search in Vim?

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.

How do I get rid of highlight in vim?

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.

What is syntax highlighting in Vim?

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.


1 Answers

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)
like image 147
Ingo Karkat Avatar answered Oct 01 '22 22:10

Ingo Karkat