Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing quote marks around strings in Vim?

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?

like image 744
ravuya Avatar asked Jan 20 '10 17:01

ravuya


People also ask

How do you remove quotation marks from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

How do you surround words with a quote in Vim?

press q and q for recording into q register (we use "q" as shortcut to remember "quotes"). press a then press ' again to surround the word with quotes.

How do you replace a single quote in a string?

Use the String. replace() method to replace single with double quotes, e.g. const replaced = str. replace(/'/g, " ); . The replace method will return a new string where all occurrences of single quotes are replaced with double quotes.


2 Answers

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

like image 117
kejadlen Avatar answered Sep 20 '22 14:09

kejadlen


You need to use groupings:

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

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

like image 44
rui Avatar answered Sep 21 '22 14:09

rui