3

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.

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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