Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Double quotes in single quotes and vice versa

Tags:

regex

vim

quotes

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?

like image 911
Reman Avatar asked Apr 12 '12 18:04

Reman


1 Answers

Typically, when you want to swap A & B like this, you need an intermediate step where you replace A with something entirely different and very likely to be unique within the document, whether an unusual character or something longer and crazier like |x-monkeyz-x|.

You can then convert all the Bs to As, and finally all the |x-monkeyz-x| to Bs.

For example,

  1. Replace all ' with !X!
  2. Replace all " with '
  3. Replace all !X! with "

EDIT

This is better: Easiest way to swap occurrences of two strings in Vim?

like image 104
Jay Avatar answered Oct 03 '22 16:10

Jay