Last Updated: February 25, 2016
·
354
· laplacesdemon

Vim Find and Replace a Part of Text

I love that vim allows us to write regular expressions that makes searching and replacing parts of the text easy.

To replace foo to bar you do this:

:%s/foo/bar/gc

This will highlight each occurance of foo and ask an action to do. Options are y to replace, q to quit, or a to yes to all.

To replace image.png to <img src="image.png" /> do following:

:%s/\(\w\+\.\w\+\)/\<img src\=\"\1\" \/\>/gc

I used \w to match alphanumeric characters and \+ to match anything after it.

For a list of available operators see: http://www.softpanorama.org/Editors/Vimorama/vim_regular_expressions.shtml