4

I am trying to replace all quotes (") with escaped quotes (\") and simultaneously all escaped quotes with normal quotes.

I found the following solution to a similar problem here for swapping 'foo' to 'bar' and vice versa:

:%s/\v(foo|bar)/\={'foo':'bar','bar':'foo'}[submatch(0)]/g

but I cannot translate it to my particular case.

The problem being that every escaped quote contains also a normal quote.

Looking forward to your suggestions.

PS: I want to avoid first replacing all escaped quotes with a different (and hopefully unique) pattern, then replacing all remaining normal quotes with escaped quotes, and then replacing the different pattern with normal quotes - but rather do it in one go.

1 Answer 1

4

I was able to use the method you found without issue. Perhaps you missed something with the escaping.

%s/\v(\\"|")/\={'\"':'"','"':'\"'}[submatch(0)]/

I "escape the escape" in the substitution pattern but not in the dictionary since the latter uses single quotes within which the text is interpreted literally.

Update: FYI, you can simplify the pattern a little, in this particular case, by using \\\?" (though that strays from the generalized solution that you found).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.