Vim replaces two words with each other

How to replace all instances of "foo" with "bar" and "bar" with "foo" in vim?

+6
source share
4 answers

Besides using a temporary word to change, you can also use the abolish plugin as follows:

:%SubVert/{foo,bar}/{bar,foo}/g 
+7
source

Look at this: how to write only one template for exchanging two lines in two directions in vim

 :s/foo\|bar/\={'foo':'bar','bar':'foo'}[submatch(0)]/g 
+10
source
  • :% s / Foo / bbaarr / g
  • :% S / bar / foo / g
  • :% s / bbaarr / foo / g

It should be the smartest way to do this, but this one will work for sure!

+1
source

You can do this using a temporary word. Just make sure it does not exist in the current document.

 /\<asd123\> :%s/\<foo\>/asd123/g :%s/\<asd123\>/bar/g :%s/\<bar\>/foo/g 
0
source

Source: https://habr.com/ru/post/908404/


All Articles