In VIM is there a regex (or any other shortcut) that will allow me to swap 2 words? [duplicate]

空扰寡人 提交于 2019-12-24 06:11:16

问题


I'm looking for a regex that'll allow me to swap between two words. (Other vim shortcuts are also acceptable)

I have a long list:

   a.transmit
   b.receive
   c.receive
   d.transmit

I need every instance of transmit to be changed to receive, and every receive to be changed to transmit so it should read:

   a.receive
   b.transmit
   c.transmit
   d.receive

Thanks,


回答1:


Yes, you can use an expression:

:%s/\(receive\|transmit\)/\=submatch(1) == 'receive' ? 'transmit' : 'receive'

It's pretty self-explanatory - if the matched group is receive the expression evalutes to transmit, otherwise to receive.




回答2:


You can do this easily with Tim Pope's Abolish plugin

:%S/{transmit,receive}/{receive,transmit}



回答3:


You can do something like this:

:%s/\v(transmit|receive)/\={'transmit':'receive','receive':'transmit'}[submatch(0)]/g



回答4:


Not a single command, but if you're willing to use sed 3 times, you can do something along the lines of

:%s/transmit/temporaryholdervalue/g
:%s/receive/transmit/g
:%s/temporaryholdervalue/transmit/g

which should switch all the values.



来源:https://stackoverflow.com/questions/17556529/in-vim-is-there-a-regex-or-any-other-shortcut-that-will-allow-me-to-swap-2-wor

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