2

我正在寻找一个允许我在两个单词之间交换的正则表达式。(其他 vim 快捷键也可以)

我有一个很长的清单:

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

我需要将传输的每个实例更改为接收,并将每个接收更改为传输,因此它应为:

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

谢谢,

4

4 回答 4

5

您可以使用 Tim Pope 的Abolish插件轻松做到这一点

:%S/{transmit,receive}/{receive,transmit}
于 2013-07-09T19:29:45.457 回答
3

你可以这样做:

:%s/\v(transmit|receive)/\={'transmit':'receive','receive':'transmit'}[submatch(0)]/g
于 2013-07-09T19:30:04.723 回答
2

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.

于 2013-07-09T19:31:57.777 回答
0

不是一个命令,但如果你愿意使用 sed 3 次,你可以做一些类似的事情

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

这应该切换所有值。

于 2013-07-09T19:29:58.163 回答