non greedy regex

Non-Greedy Matching in Vim

In Vim, when using regular expressions, it's often useful to make matches non-greedy. This means the pattern will match as little text as possible rather than as much as possible.

Instead of using .* which is greedy and matches as much as possible, use .\{-} which is non-greedy and stops at the first opportunity.

Example:

%s/=".\{-}"/=""

This replaces everything between quotes, but only up to the first closing quote, not the last one.

See :help non-greedy for more information.


edit this page