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

How do I temporarily highlight a custom text pattern in my buffer without changing the syntax file?

Answer

:match {group} /{pattern}/

Explanation

:match lets you apply a highlight group to any pattern in the current window without touching the buffer or its syntax rules. It's a quick way to draw attention to a specific pattern — TODOs, a variable name, overly long lines — during a session.

How it works

  • :match {group} /{pattern}/ applies the named highlight group to all matches of {pattern} in the active window
  • :match (no arguments) or :match none clears the match
  • Vim provides two additional layers: 2match and 3match, giving you up to three independent concurrent highlights
  • Groups like Error, Todo, Search, WarningMsg, or any custom highlight group can be used

Example

Highlight all occurrences of TODO in red using the built-in Todo group:

:match Todo /TODO/

Highlight lines longer than 80 characters:

:match ErrorMsg /\%>80v./

Use a second layer for a different pattern simultaneously:

:2match WarningMsg /FIXME/

Clear all custom matches:

:match none
:2match none

Tips

  • Matches are window-local, so different windows can show different highlights
  • :highlight lists all available groups you can pass to :match
  • For persistent per-filetype highlights, prefer syntax match in a syntax file; use :match for quick ad-hoc sessions

Next

How do I match a pattern only when it is preceded or followed by another pattern, without including that context in the match?