单引号中的双引号,反之亦然

时间:2022-09-15 13:37:18

Often I find myself inverting quotes:
from double quotes "" to single quotes '' and
from single quotes '' to double quotes "".

我经常发现自己在颠倒引号:从双引号到单引号,从单引号到双引号。

I know there is a way to switch single quotes to double quotes:
:%s/'\(\([^']*\)\)'/"\1"/g

我知道有一种方法将单引号,双引号::% s / \ \([^]* \)\)”/“\ 1”/ g

And a way to switch double quotes to single quotes:
:%s/"\(\([^"]*\)\)"/'\1'/g

和双引号转换为单引号::% s /“\ \([^]* \ \)”/ \ 1 / g

but how do I do both operations together without including the first swapped quotes in the 2nd swapping?

但是,如何在不包含第二次交换中交换的第一个引号的情况下同时执行这两个操作呢?

4 个解决方案

#1


3  

If there is no escaped quotes inside string literals and it is not needed to ensure correct pairing of quotes, one can use the command

如果字符串文字中没有转义引号,并且不需要确保正确的引号配对,可以使用该命令

:%s/['"]/\="'\""[submatch(0)!='"']/g

#2


5  

Typically, when you want to swap A & B like this, you need an intermediate step where you replace A with something entirely different and very likely to be unique within the document, whether an unusual character or something longer and crazier like |x-monkeyz-x|.

通常,当您想要这样交换A和B时,您需要一个中间步骤,用完全不同的东西替换A,并且很可能在文档中是唯一的,不管是不寻常的字符,还是比|x-monkeyz-x|更长更疯狂的东西。

You can then convert all the Bs to As, and finally all the |x-monkeyz-x| to Bs.

然后你可以把所有的Bs转换成As,最后所有的|x-monkeyz-x|转换成Bs。

For example,

例如,

  1. Replace all ' with !X!
  2. 用!
  3. Replace all " with '
  4. 用"替换"
  5. Replace all !X! with "
  6. 替换所有X !以“

EDIT

This is better: Easiest way to swap occurrences of two strings in Vim?

这更好:在Vim中交换两个字符串的最简单方式?

#3


2  

Use \=:

使用\ =:

:%s/'\([^']*\)'/\='"'.tr(submatch(1), '"', "'").'"'/g

. This assumes that both characters only serve as quotes, but your initial code also does the same, except that my does not check for them being paired.

。这假设两个字符都只作为引号,但是您的初始代码也一样,只是我不检查它们是否成对。

#4


2  

I usually use an intermediate string like my name that's unlikely to appear in the text:

我通常使用中间字符串,比如我的名字,不太可能出现在文本中:

  • Change single quote to UNLIKELY_STRING
  • 将单引号更改为不太可能的_string
  • Change double quote to single quote
  • 将双引号改为单引号
  • Change UNLIKELY_STRING to double quote
  • 将不太可能的_string更改为双引号

#1


3  

If there is no escaped quotes inside string literals and it is not needed to ensure correct pairing of quotes, one can use the command

如果字符串文字中没有转义引号,并且不需要确保正确的引号配对,可以使用该命令

:%s/['"]/\="'\""[submatch(0)!='"']/g

#2


5  

Typically, when you want to swap A & B like this, you need an intermediate step where you replace A with something entirely different and very likely to be unique within the document, whether an unusual character or something longer and crazier like |x-monkeyz-x|.

通常,当您想要这样交换A和B时,您需要一个中间步骤,用完全不同的东西替换A,并且很可能在文档中是唯一的,不管是不寻常的字符,还是比|x-monkeyz-x|更长更疯狂的东西。

You can then convert all the Bs to As, and finally all the |x-monkeyz-x| to Bs.

然后你可以把所有的Bs转换成As,最后所有的|x-monkeyz-x|转换成Bs。

For example,

例如,

  1. Replace all ' with !X!
  2. 用!
  3. Replace all " with '
  4. 用"替换"
  5. Replace all !X! with "
  6. 替换所有X !以“

EDIT

This is better: Easiest way to swap occurrences of two strings in Vim?

这更好:在Vim中交换两个字符串的最简单方式?

#3


2  

Use \=:

使用\ =:

:%s/'\([^']*\)'/\='"'.tr(submatch(1), '"', "'").'"'/g

. This assumes that both characters only serve as quotes, but your initial code also does the same, except that my does not check for them being paired.

。这假设两个字符都只作为引号,但是您的初始代码也一样,只是我不检查它们是否成对。

#4


2  

I usually use an intermediate string like my name that's unlikely to appear in the text:

我通常使用中间字符串,比如我的名字,不太可能出现在文本中:

  • Change single quote to UNLIKELY_STRING
  • 将单引号更改为不太可能的_string
  • Change double quote to single quote
  • 将双引号改为单引号
  • Change UNLIKELY_STRING to double quote
  • 将不太可能的_string更改为双引号