Vim finding and replacing without captures

Normally when doing find and replaces in a file there are some key words that we want to key off, but not replace. This can be a pain because you have to then isolate the part you want to change, capture everything but the changing part, and then reconstruct it in the substitution. Well there is a better way…

If your goal is to prepend all of the name=”bad-.*” with name=”good-.*”, then we would normally have to write something like:

:s/\(name="\)bad\(-.*"\)/\1good\2/g

There is a better way using zoom start ‘\zs’ and zoom end ‘\ze’:

:s/name="\zsbad\ze-/good/g

Happy finding and replacing!

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s