Thursday, December 20, 2012

Vim search/replace

sure, you know about s/foo/bar/ which replaces bar where foo exists.

Let's say you want to find foo but only when it comes before bar, and you want to replace some but not all of it.

s/\(foo\)bar/\1goo/
Will replace foobar with foogoo
will keep foodoo as  is.

You can use this to, for instance, insert something before a comma:
s/\([a-z]\),/\1 = 0,/
foo, is now foo = 0,
or insert something between foo and bar
s/\(foo\)\(bar\)/\1 nutty \2/
changes foobar to foo nutty bar

No comments:

Blog Archive