Double quotes in single quotes and vice versa

quotes, regex, vim

Solution

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

Problem

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` And a way to switch double quotes to single quotes: `:%s/"\(\([^"]*\)\)"/'\1'/g` but how do I do both operations together without including the first swapped quotes in the 2nd swapping?

Original source

Related problems