问题
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