0
  1. I select the whole contents of a source buffer: ggVGy.
  2. Then I move to another buffer, let's say D1.
  3. I update the the whole contents of the destination buffer with the yanked text: ggVGp.

Now, if I try to repeat the last two steps on a new destination buffer, let's say D2, I see I have lost the original source text. Instead, what I have in the clipboard are the contents of the last destination buffer, D1.

  • Is there a way I can repeatedly yank the same source text to different destination buffers?
  • What can be the reason to this behaviour? May I have a setting saying something like "selecting text automatically yanks it"? If so, I think I would like to disable that behaviour.
1
  • This is a frequent source of confusion :) I wish we had a better reference Q, but romainl's answer below covers most of the important parts.
    – D. Ben Knoble
    Commented Dec 25, 2022 at 22:54

1 Answer 1

2

What can be the reason to this behaviour?

The rationale is explained under :help v_p:

With p the previously selected text is put in the unnamed register (and
possibly the selection and/or clipboard).  This is useful if you want to put
that text somewhere else.  But you cannot repeat the same change.

Is there a way I can repeatedly yank the same source text to different destination buffers?

Look no further than the next paragraph of the same help section, which gives you two methods:

With P the unnamed register is not changed (and neither the selection or
clipboard), you can repeat the same change. But the deleted text cannot be
used.

and:

If you do need it you can use p with another register.  E.g., yank
the text to copy, Visually select the text to replace and use "0p .  You can
repeat this as many times as you like, and the unnamed register will be
changed each time.

So, you would do it like this with the first method:

ggVGy
:bnext
ggVGP
:bnext
ggVGP

and like this with the second method:

ggVGy
:bnext
ggVG"0p
:bnext
ggVG"0p

Note that you can use yet another vanilla method: yanking to/putting from a named register:

ggVG"ay
:bnext
ggVG"ap
:bnext
ggVG"ap

See :help registers for "a and "0.

3
  • You know this, but :%yank/:%yank a and :%delete | put 0/:%delete | put a are also options with convenient shorthands.
    – D. Ben Knoble
    Commented Dec 25, 2022 at 22:52
  • Yes I do, and there are other methods, even. But I wanted to stay within the scope of the question.
    – romainl
    Commented Dec 26, 2022 at 7:48
  • Many thanks for the detailed explanation. Appreciated!
    – rturrado
    Commented Dec 26, 2022 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.