find and replace exact word in vim

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
This entry was posted in coding, tech, vim and tagged . Bookmark the permalink.

please comment

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s