Vim: lazy match on wildcard
more from dev
May 8, 08

Got a question about vim pattern matching that I had to look up, probably worth sharing.

Say you have the following text, and you want to modify the quoted things, so you start with this:

blah blah blah "BLAH" blah blah "FOO" blah blah

And want to end up with this:

blah blah blah <"BLAH"> blah blah <"FOO"> blah blah

Run this:

:%s/\(".\{-\}"\)/<\1>/g

I expected ".*?" to work (valid Perl regex syntax?), but ultimately found ".{-}" worked with Vim (and don't forget to escape the "{" and "}" characters, so \{ and \}).