Reputation: 103
How would one replace all instances of 'foo' with 'bar' and 'bar' with 'foo' in vim?
Upvotes: 9
Views: 2352
Reputation: 161674
Take a look at this: how to write only one pattern to exchange two strings in two-ways in vim
:s/foo\|bar/\={'foo':'bar','bar':'foo'}[submatch(0)]/g
Upvotes: 13
Reputation: 40384
Aside from using a temporary word for the change, you could also use abolish plugin like this:
:%SubVert/{foo,bar}/{bar,foo}/g
Upvotes: 8
Reputation: 1882
You can do it using temp word. Just be sure that it doesn't exists in the current document.
/\<asd123\>
:%s/\<foo\>/asd123/g
:%s/\<asd123\>/bar/g
:%s/\<bar\>/foo/g
Upvotes: 0
Reputation: 3604
It must exist an smartest way to do it, but this one will work for sure !
Upvotes: 1