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

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

  • \v enables very-magic mode so the pattern is easier to read
  • start is your opening marker
  • \_. means any character including a newline (. plus line breaks)
  • {-} is Vim's non-greedy quantifier, so it expands as little as possible
  • end is 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 cgn for targeted, repeatable edits on each matched block.

Next

How do I paste the unnamed register after transforming it to uppercase?