tags:

views:

1037

answers:

10

Lets say that i have this text:

$test = 'lorem';
$test2= 'ipsum';

and I want to copy lorem and paste into ipsum. I tried to do yi' on lorem and then went on ipsum and did ci' but that replaced my pastebin with ipsum. and my previous copy was lost.

+1  A: 

Why don't you yank into a named buffer, using "ayi', then delete and paste with d'i"aP?

Paul Tomblin
+3  A: 

I usually go to the sed command.

:%s/ipsum/lorem/g
  • % means do this for every line
  • s means sed, or search and replace
  • g at the end means replace every ipsum with lorem; if you omit this, it only replaces the first.
Scottie T
i dont want to use replace since that means i have to type it
solomongaby
A little bit of work if you don't want to type either word, but you can use C-R C-W to paste the word under the cursor, e.g.: :%s/<C-R><C-W>/<ESC>, move cursor to ipsum, :<UP><C-R><C-W>/g
Andrew Coleson
+1  A: 

I'm not sure what do you want exactly.

You have this on two lines:

$test = 'lorem';
$test2= 'ipsum';

If you go to l and yw, it yanks lorem, then go to i of ipsum, and cw, and p and it will replace ipsum with lorem. You will still have both lorem and ipsum in the registers.

You can get the contents of those registers with :reg

If you want to paste something from them then "*p, or ":p, or "0p (you'll see it when you type :reg)

ldigas
A: 

You want to use y to copy a selection and p to paste.

Here is a good list to keep handy.

Andrew Hare
+4  A: 

yi' on lorem, move to i of ipsum, vep?

chaos
thanks this is what i was looking for :D
solomongaby
You're welcome. :)
chaos
Ok, but now the register contains ipsum.
Michiel de Mare
Hm, so it does. Experiments with . show that it implements the paste-replace using a delete, which puts ipsum in your default register. Seems this is hard to prevent; http://stackoverflow.com/questions/290465/vim-how-to-paste-over-without-overwriting-register has a hideously involved solution.
chaos
Easy to solve:"kyi' on lorem, move to i of ipsum, ve"kp?This yanks lorem into register k, pastes it over ipsum from register k, and keeps it in register k ready to paste again anywhere else you might want to put it. ipsum still ends up in the default register, but that's no longer a problem and could be useful too.
sampablokuper
Oops, typo in the above. That should have been:"kyi' on lorem, move to i of ipsum, ve"kp
sampablokuper
I've copied my solution to a new answer; feel free to vote it up!
sampablokuper
A: 

Words here to stop Markdown treating the code that follows as plain text.

/lorem
"adw
/ipsum
"aP"bdw
``
"bp

The first text searches for 'lorem'; the next deletes the word into the buffer named 'a', leaving a pair of empty quotes behind in the text. The next search finds 'ipsum'; the "aP pulls the buffer named 'a' in before the word ipsum; the "bdw deletes the word into the buffer named 'b', leaving 'lorem' behind. The double back-tick goes back to the place the last search came from - the empty quotes; and "bp pulls the named buffer after the first quote.

You can also omit the "a and "b, but this way, the values are in the named buffers "a and "b and can be copied again, and again, and again until you don't need the values any more.

Jonathan Leffler
+2  A: 

Go to the l of lorem, ye (yank to end of word). Go to the i of ipsum, "_de (delete to end of word, putting the deleted text in the black hole register. P (paste register before cursor).

Altogether: yej"_deP

Michiel de Mare
A: 

After you do ci' on impsum your lorem is in register "0. So, you can do ci'^R0 (^R means Ctrl+r) and paste your lorem in place of ipsum.

See :help quote_number for more info on numbered registers.

Paul
A: 

vi'y on lorem

vi'p on ipsum

gvy to copy back lorem to register for possible macro with vi'p

(qa - gvy - j - vi'p - q - @a - @@ - @@ ...)

+4  A: 

Easy:

"kyi' on lorem, move to i of ipsum, ve"kp

This yanks lorem into register k, pastes it over ipsum from register k, and keeps it in register k ready to paste again anywhere else you might want to put it. ipsum still ends up in the default register, but that's no longer a problem and could be useful too.

If you've already got something in register k, you can use a different register instead (just use a different key).

sampablokuper