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

How do I search for a pattern that spans multiple lines?

Answer

/pattern\n next-line

Explanation

Using \n in a Vim search pattern matches a newline character, allowing you to find patterns that cross line boundaries.

How it works

  • \n matches a newline (end of line followed by start of next line)
  • \_s matches whitespace including newlines
  • \_.* matches any characters including newlines (multi-line wildcard)

Example

Search for a closing brace followed by an opening brace:

/}\n{        " matches } at end of line, { at start of next

Search for if followed by else on a subsequent line:

/if\_.*else   " matches across any number of lines

Tips

  • \_s is "whitespace including newline"
  • \_. is "any character including newline"
  • \_[] is a character class including newline
  • Multi-line patterns can be slow on large files
  • Use \n in the search but \r in substitution replacements

Next

How do you yank a single word into a named register?