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
\nmatches a newline (end of line followed by start of next line)\_smatches 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
\_sis "whitespace including newline"\_.is "any character including newline"\_[]is a character class including newline- Multi-line patterns can be slow on large files
- Use
\nin the search but\rin substitution replacements