I have a situation with typesetting a book in which the quotation marks are all screwed up. The author used a variety of marks¹ and apparently didn't believe in discriminating between them. I've managed to sort out a lot of them such as fixing all the uses as apostrophe and most of the opening and closing quotes, but there are a few lingering problems. I have ten more open curly quotes () that closing curly quotes (). This means that somewhere either quotations aren't closed at all or the wrong mark is used. Searching for one at a time (or even both at once with a regex) helps a little, but still visually scanning hundreds of pages of text for miss-matches is a lot of work.

Is there a way that I can search for two expressions at once and highlight each in a different color? This way I could skim for pairs without having to look closely at each one.

¹ I kid you not there was a random distribution of single and double straight quotes, single and double curly quotes in assorted directions, back-ticks, and various combinations including '', '‘, ‘', ’‘, etc.


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 '"'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.