vim copy and replace text

2019-03-27 07:00发布

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.

10条回答
手持菜刀,她持情操
2楼-- · 2019-03-27 07:13

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.

查看更多
Root(大扎)
3楼-- · 2019-03-27 07:14

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

查看更多
成全新的幸福
4楼-- · 2019-03-27 07:18

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.

查看更多
5楼-- · 2019-03-27 07:20

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 - @@ - @@ ...)

查看更多
闹够了就滚
6楼-- · 2019-03-27 07:24

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

Here is a good list to keep handy.

查看更多
狗以群分
7楼-- · 2019-03-27 07:25

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.
查看更多
登录 后发表回答