Replacing quote marks around strings in Vim?

↘锁芯ラ 提交于 2019-12-02 15:11:44

You need to use groupings:

:s/\'\(.*\)\'/\"\1\"

This way argument 1 (ie, \1) will correspond to whatever is delimited by \( and \).

There's also surround.vim, if you're looking to do this fairly often. You'd use cs'" to change surrounding quotes.

%s/'\([^']*\)'/"\1"/g

You will want to use [^']* instead of .* otherwise

'apples' are 'red' would get converted to "apples' are 'red"

unless i'm missing something, wouldn't s/\'/"/g work?

Just an FYI - to replace all double quotes with single, this is the correct regexp - based on rayd09's example above

:%s/"\([^"]*\)"/'\1'/g

You need to put round brackets around the part of the expression you wish to capture.

s/\'\(.*\)\'/"\1"/

But, you might have problems with unintentional matching. Might you be able to simply replace any single quotes with double quotes in your file?

You've got the right idea -- you want to have "\1" as your replace clause, but you need to put the "Hello There" part in capture group 1 first (0 is the entire match). Try:

:%/'\(.*\)'/"\1"

Eddie B

Presuming you want to do this on an entire file ...

N Mode:

ggvG$ [SHIFT+:]  

X Mode:

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