How do I make the dot (.) in a Vim search pattern match newline characters too?
Answer
/_.
Explanation
In Vim's regex, . matches any character except a newline. To match any character including newlines, prefix it with \_ to get \_.. This lets you write patterns that span multiple lines — matching across line breaks that . alone would miss.
How it works
.— matches any single character except newline\_.— matches any single character INCLUDING newline\_can prefix any character class atom to add newline matching:\_s— whitespace including newlines\_w— word character or newline\_[]— character class including newline
Examples
Match if followed by then with anything (including newlines) between:
/if\_.*then
Match a function signature that may span multiple lines:
/function\_s\+\w\+(\_.*)\n
Replace newlines between two patterns (join lines):
:%s/start\zs\_.*\ze end/ MIDDLE /
The \_ family
| Pattern | Matches |
|---|---|
\_s |
Whitespace or newline |
\_S |
Non-whitespace (but not newline) |
\_w |
Word character or newline |
\_. |
Any character or newline |
\_[a-z] |
Lowercase letter or newline |
Tips
\_.*is greedy and can match enormous spans of text — use\_.\{-}for non-greedy matching across lines- This is Vim-specific syntax — PCRE uses the
/sflag to make.match newlines; Vim uses the\_prefix instead - Combine with
\zsand\zeto set match boundaries within a multi-line pattern :help /\_documents the full collection of newline-inclusive atoms