How do I search non-greedily across multiple lines between two markers?
Answer
/\vstart\_.{-}end
Explanation
Multiline searches in Vim often overmatch because .* is greedy and, by default, . does not cross line breaks. This pattern uses Vim-specific atoms to match across lines and stop at the nearest ending marker. It is especially useful when inspecting logs, config blocks, or generated files where start and end delimit sections you need to review quickly.
How it works
\venables very-magic mode so the pattern is easier to readstartis your opening marker\_.means any character including a newline (.plus line breaks){-}is Vim's non-greedy quantifier, so it expands as little as possibleendis your closing marker
Together, /\vstart\_.{-}end finds the shortest match from start to the next end, not the farthest one in the file.
Example
start
alpha
end
...
start
beta
gamma
end
Searching with /\vstart\_.{-}end will first match only the first block (start ... end). Press n to jump to the next nearest block.
Tips
- Replace literal markers with real patterns, for example
/\vBEGIN\_.{-}^END$/. - If you need the widest block instead, switch to greedy
\_.*. - Combine with
cgnfor targeted, repeatable edits on each matched block.