Replacing quote marks around strings in Vim?

39,288

Solution 1

You need to use groupings:

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

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

Solution 2

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

Solution 3

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

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

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

Solution 4

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

Solution 5

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

:%s/"\([^"]*\)"/'\1'/g
Share:
39,288
ravuya
Author by

ravuya

Updated on June 17, 2020

Comments

  • ravuya
    ravuya almost 4 years

    I have something akin to <Foobar Name='Hello There'/> and need to change the single quotation marks to double quotation marks. I tried :s/\'.*\'/\"\0\" but it ended up producing <Foobar Name="'Hello There'"/>. Replacing the \0 with \1 only produced a blank string inside the double quotes - is there some special syntax I'm missing that I need to make only the found string ("Hello There") inside the quotation marks assign to \1?