在 Vim 中交换两个字符串的最简单方法是什么?
将所有出现的 string_a
替换为 string_b
同时将任何已经是 string_b
的内容更改为 string_a 的最简单方法是什么?我当前的方法如下:
:s/string_a/string_c/g
:s/string_b/string_a/g
:s/string_c/string_b/g
虽然这有效,但它需要额外的输入并且似乎效率低下。有谁知道更好的方法来做到这一点?
What is the easiest way to replace all occurrences of string_a
with string_b
while at the same time changing anything that was already string_b
into string_a
? My current method is as follows:
:s/string_a/string_c/g
:s/string_b/string_a/g
:s/string_c/string_b/g
Although this works, it requires extra typing and seems inefficient. Does anybody know of a better way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会这样做:
但是打字太多了,所以我会这样做:
但这仍然需要输入
foo
和bar
两次,所以我会这样做像这样的事情:如果你的一个单词包含
/
,你必须传入一个你知道你的单词都不包含的分隔符,.eg这还有一个好处就是能够交换多个对一次的言语。
I'd do it like this:
But that's too much typing, so I'd do this:
But that still requires typing
foo
andbar
twice, so I'd do something like this:If one of your words contains a
/
, you have to pass in a delimiter which you know none of your words contains, .e.gThis also has the benefit of being able to swap multiple pairs of words at once.
您可以使用 Tim Pope 的 Abolish 插件轻松完成此操作
You can do this easily with Tim Pope's Abolish plugin
这是我如何交换两个单词
skip
&limit
:很确定有人可以将其变成一个更短的命令,它接受两个参数。
Here is how I swap two words
skip
&limit
:Pretty sure someone could turn it into a shorter command which accepts two arguments.
swapstrings 插件为此提供了一个方便的命令:
The swapstrings plugin provides a handy command for this:
您可以使用单个命令来完成此操作,如下面的代码所示:
You can do it with a single command as shown in my code below:
下面是如何交换一行内由空格键分隔的 2 个字符串的示例。可以使用:
Here is an example how to swap 2 strings separated by space bar within one line. One can use: