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 noneclears the match- Vim provides two additional layers:
2matchand3match, giving you up to three independent concurrent highlights - Groups like
Error,Todo,Search,WarningMsg, or any customhighlightgroup 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
:highlightlists all available groups you can pass to:match- For persistent per-filetype highlights, prefer
syntax matchin a syntax file; use:matchfor quick ad-hoc sessions