Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim replace two words with one another

Tags:

command

vim

vi

How would one replace all instances of 'foo' with 'bar' and 'bar' with 'foo' in vim?

like image 595
Jason McGhee Avatar asked Feb 14 '12 07:02

Jason McGhee


2 Answers

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
like image 86
kev Avatar answered Sep 28 '22 05:09

kev


Aside from using a temporary word for the change, you could also use abolish plugin like this:

:%SubVert/{foo,bar}/{bar,foo}/g
like image 20
jcollado Avatar answered Sep 28 '22 03:09

jcollado