Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can replace string in quotes with string from buffer?

Tags:

vim

copy-paste

I'm doing the following to copy some text inside quotes and paste it in a different place (inside quotes as well):

di"
go-to-buffer-for-copy
copy-string (ex. yi")
then-go-to-prev-buffer
paste-to-string (p)

But I want to do it in a simpler way, like this:

yi"
go-to-buffer-for-paste
replace-inner-quotes-to-yanked-text
like image 518
RusAlex Avatar asked Dec 19 '10 15:12

RusAlex


People also ask

How do you replace a quote in C#?

s = s. Replace(@"""", @"\"""); In the first example the " has to be escaped with a backslash as it would otherwise end the string. Likewise, in the replacement string \\ is needed to yield a single backslash by escaping the escape character.

How do you replace a double quote in Vim?

You'd use cs'" to change surrounding quotes. Save this answer.

How do you remove a quote from a string?

Use the String. replaceAll() method to remove all double quotes from a string, e.g. str. replaceAll('"', '') . The replace() method will return a new string with all double quotes removed.


2 Answers

For "replace-inner-quotes-to-yanked-text" you can use vi"p.

like image 108
Randy Morris Avatar answered Nov 10 '22 00:11

Randy Morris


(pulling my comment into its own answer)

Assuming you use vim with system clipboard, you could do the following:

  • "+yi" to copy the text inside quotes to your system clipboard
  • position your cursor inside the quotes where you want to put that text
  • ci"<Ctrl-V><Esc> replaces what's inside the quotes with the content of your clipboard

One benefit is that if you want to put the original text in multiple places, you can place your cursor in the next position and press ..

vi"p, proposed by Randy Morris, works but it replaces the content of your default register and the selection won't be captured in the "do again" command, only the paste.

You could still use the "0 register to access your original text but I haven't found a command that would change inside the quotes and paste in a way that redo maintains.

like image 20
Timothée Boucher Avatar answered Nov 10 '22 00:11

Timothée Boucher