I don't know if it's what you're looking for, but let's say you've got two patterns foo
and bar
, that you want to highlight in different colors.
First you need to define two highlight groups to set up the colors you want. Let's say, one with green background and the other one with blue background :
:highlight MyGroup1 ctermbg=green guibg=green
:highlight MyGroup2 ctermbg=blue guibg=blue
Next you need to define your two matches with the matchadd()
function.
For example, you could apply the green background defined in the highlight group MyGroup1
to the pattern foo
and the blue background defined in MyGroup2
to the pattern bar
:
:call matchadd("MyGroup1", "foo")
:call matchadd("MyGroup2", "bar")
You can access all the information (highlight group, pattern, priority, id) about your matches with the getmatches()
function:
:echo getmatches()
The id
key is useful because its value allows you to delete a specific match with the matchdelete()
function :
:call matchdelete({id})
Besides, if you store the output of getmatches()
inside a variable (let's say m
) :
:let m = getmatches()
... then you can clear your matches with the clearmatches()
function :
:call clearmatches()
... and restore them with the setmatches()
function afterwards :
:call setmatches(m)
Finally, if you have to define those matches regularly, you can gain some time by storing the commands that define them in a separate file and source it when needed.
For example, you could store the following code in mymatches.vim
:
highlight MyGroup1 ctermbg=green guibg=green
highlight MyGroup2 ctermbg=blue guibg=blue
call matchadd("MyGroup1", "foo")
call matchadd("MyGroup2", "bar")
And source it with : :so mymatches.vim
Note that if one of your pattern is a double quote "
, you should escape it "\""
or put single quotes around '"'
.