Vim replace two words with one another

怎甘沉沦 提交于 2019-12-07 03:01:04

问题


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


回答1:


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

:%SubVert/{foo,bar}/{bar,foo}/g



回答2:


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



回答3:


  1. :%s/foo/bbaarr/g
  2. :%s/bar/foo/g
  3. :%s/bbaarr/foo/g

It must exist an smartest way to do it, but this one will work for sure !




回答4:


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


来源:https://stackoverflow.com/questions/9273377/vim-replace-two-words-with-one-another

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!