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!
Categories