vimtricks.wiki Concise Vim tricks, one at a time.

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 :2match and :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 :match does not affect :2match or :3match
  • :match is 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, and WarningMsg are built-in; check :hi for the full list

Next

How do I configure Vim's command-line tab completion to show all matches and complete to the longest common prefix?