How do I highlight multiple different patterns simultaneously using Vim's built-in match commands?
Answer
:2match Todo /FIXME/
Explanation
Vim provides three independent match slots — :match, :2match, and :3match — each of which highlights a pattern using a specified highlight group. Using all three lets you color up to three different patterns at the same time without any plugins or matchadd() calls.
How it works
:match {Group} /{pat}/— fills slot 1:2match {Group} /{pat}/— fills slot 2:3match {Group} /{pat}/— fills slot 3:match(no arguments) — clears slot 1; same for:2matchand:3match- Each slot is per-window and persists independently
- Matches in slot 1 take highest priority; slot 3 takes lowest
Example
Highlight TODOs, FIXMEs, and HACKs simultaneously with distinct colors:
:match Search /TODO/
:2match Todo /FIXME/
:3match Error /HACK/
Now all three patterns appear in different colors across the buffer.
To clear a specific slot:
:2match
Tips
- Slots are independent: clearing
:matchdoes not affect:2matchor:3match :matchis also cleared whenever you switch colorschemes (:colorscheme)- For more than three simultaneous highlights, use
matchadd()instead — it supports unlimited matches with configurable priority - Highlight group names like
Search,Todo,Error,Comment, andWarningMsgare built-in; check:hifor the full list