How do I search for text that simultaneously matches two different patterns using Vim's AND operator?
Answer
/pattern1\&pattern2
Explanation
Vim's \& operator lets you intersect two patterns so the match must satisfy both simultaneously. Unlike \| (OR), which finds text matching either branch, \& requires the matched text to satisfy all branches at once — the final match is the text captured by the rightmost branch.
How it works
/A\&B— matches text that satisfies bothAandBat the same position- The full extent of
Ais checked against the start of the match, but the matched text is whatBcaptures - This makes
\&ideal for adding constraints like line length, context, or content requirements
Example
Find TODO comments only on lines longer than 80 characters:
/.\{80,\}\&.*TODO
This matches TODO (the right branch), but only where the same line also satisfies .\{80,\} (80+ characters from the line start).
Another example — find standalone numbers (words made entirely of digits):
/\<\w\+\>\&\d\+
This matches only words that are also pure digit sequences, filtering out words like abc123.
Tips
- Use
\&to layer conditions that are hard to express in a single branch - Multiple
\&operators can be chained:/A\&B\&C - In very-magic mode (
\v),&has special meaning (repeat of previous match), so use\&explicitly or use\mmagic mode for\& - See
:help /\&for the full specification