find and replace exact word in vim

Posted by

I have spent the weekend updating some old code, and as usual it’s a bit of a mess … One of the problems I have had is that there is lots of stuff that have similar names, but do different things, so I have to be careful about bulk search-replace commands.

Happily, vim has a neat search and replace command that narrows the case to exact word / string matches: it’s

`:s/\<OLDSTRING\>/NewString/g`

Say we have the following (i have lots just like it):

rbaCash.test <- …

rbaCash.test.irf <- …

rbaCash.test2 <- …

rbaCash.test2.irf <- …

And I wish to change `rbaCash.test` to `rbaCash.testOld`.

Using `%s/\<rbaCash.test\>/rbaCash.testOld/g` changes only the `rbaCash.test` strings and not the `rbaCash.test2` strings — which is what i want.

This is a big improvement on the standard search-replace command:

`%s/rbaCash.test/rbaCash.testOld/g`

erroneously changes the `rbaCash.test2` strings to `rbaCash.testOld2` …

Advertisements