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

How do I write a search pattern that requires two conditions to match at the same position in Vim?

Answer

\&

Explanation

Vim's \& operator is the AND combinator in a search pattern. Both branches separated by \& must match at the same position for the overall pattern to succeed. This is different from alternation (\|) which succeeds when either branch matches — \& requires that ALL branches match simultaneously.

How it works

The syntax is:

/branch1\&branch2

Vim tries to match each branch at the current position. If every branch matches, the overall match spans the region covered by the last branch.

  • Each branch is evaluated independently at the same starting position.
  • The match region returned is that of the final (rightmost) branch.
  • You can chain multiple conditions: /a\&b\&c requires all three to match.

Example

To find lines that are longer than 80 characters AND contain the word TODO:

/\v.{81,}&.*TODO.*

Breaking it down:

  • .{81,} — matches lines with at least 81 characters
  • .*TODO.* — matches lines containing TODO

Only lines satisfying both conditions are highlighted.

Another example — find words that are at least 8 letters long AND start with a capital:

/\<\u\w\{7,\}\>\&\w\+

Tips

  • \& is a zero-overhead alternative to lookaheads: /foo\&.\{4,\}/ matches foo only when it is at least 4 characters long.
  • Combine with \v (very magic) for cleaner syntax: /\v(pattern1)&(pattern2).
  • Use it in :g commands to act on lines satisfying multiple criteria without chaining multiple :g calls.

Next

How do I remove a word I accidentally added to my Vim spell dictionary?