49

Here is my problem:

I am in visual mode.

I select text and copy it to the buffer. ((y)ank)

I select another text which I want to replace and paste the buffer. ((p)aste)

Now the second selection has been replaced in the buffer, however I want the first one to still sit in there.

Any ideas?

Juergen Riemer
  • 1,393
  • 2
  • 13
  • 29
  • 1
    Possible duplicate of [How to paste over without overwriting register](https://stackoverflow.com/questions/290465/how-to-paste-over-without-overwriting-register) – Kevin Ji Dec 08 '18 at 20:02

2 Answers2

43

Often, this behavior is useful. When you don't want it, you can instead do the usual yank, then paste (officially, 'put') with "0p. You can do this however many times you like.

See :help v_p for more.

If you want to avoid the overwrite, you need to delete first. You can use "_ to select the blackhole buffer, then delete d, then paste before P and you'll avoid the buffer being set.

Peter
  • 127,331
  • 53
  • 180
  • 211
  • Thanks Peter, I see, you use an explicit buffer to do the trick. Is there a way/setting to avoid the automatic population of the buffer by mere selection in the first place? – Juergen Riemer Oct 01 '10 at 09:45
  • unfortunately not (see the help mentioned). also, it's not the selection that does the setting, it's the pasting over the selection. I've added an alternative method to my answer though. – Peter Oct 01 '10 at 09:59
  • I see, well then your first suggestion seems to be the best way to do it. Thanks! – Juergen Riemer Oct 01 '10 at 10:19
  • 7
    You can make both of these the default by remapping the "p" command in visual mode. The first option would be: `vnoremap p "0p`. This uses the "0" register (most recent yank) when putting in visual mode. The unname register is still overwritten, but subsequent puts (in visual mode) do not use it. This does mean that you have to yank first, deleting and then putting (in visual mode) does no longer work. The second option is (IMHO) better: `vnoremap p "_dP` and remaps p to first delete into the blackhole register and then do a normal put before. This should be exactly what you're asking for. – Matthijs Kooijman Jul 09 '17 at 10:11
  • 17
    I have never found the default behaviour of vim useful. a "put" should not be a "copy and put". – stimulate Jul 09 '18 at 12:50
  • @stimulate It sounds like your real gripe is that "delete" is actually "cut". The line has to go somewhere! – preferred_anon Mar 18 '19 at 11:52
  • 2
    @preferred_anon no, when you copy something (or "yank" it) and then select something else and replace that with p (by pasting what you copied), p will also replace what you copied before with what you're replacing. So to replace multiple places with the same thing, you have to delete each place into void register with "_d and then you can paste with p. But p should just paste imho, if you want to copy before, you should just copy before. The line is not supposed to go anywhere if I am replacing it. – stimulate Mar 18 '19 at 12:04
  • @stimulate That's precisely my point: In vim, deleting something *always* copies what you deleted. Pressing `p` in visual mode deletes the selected text, and it does that in the default manner: by cutting. What I'm trying to say is that `v_p` has not been given some strange pathological behaviour in spite of vim's ordinary intuitive deleting - it's been given the default behaviour in line with what `d` and `x` do everywhere (and that's what you seem to dislike) – preferred_anon Mar 18 '19 at 12:09
  • @preferred_anon Yes, my point is that this somewhat contradicts the mnemonic quality of the commands. But there are many other cases where the hotkeys do something else than what they are called. Athough c does cut, the reason you use it over d (which is effectively cutting) is because it jumps directly into insert mode. So it is actually cut and insert, and d is actually cut. "_d is delete. I have gotten used it, but I can imagine we could do better... – stimulate Mar 18 '19 at 14:53
  • 1
    @stimulate I'm definitely sympathetic to that. One thing that may help you with `c`: It's supposed to stand for `change`, which is why it puts you in insert mode afterwards. – preferred_anon Mar 18 '19 at 14:55
  • Great. Thank you. – Pasha Sep 13 '19 at 22:20
2

While this does not technically answer the question (not using default buffer) it does solve the symptom of the problem so I thought I would still share. I get around this issue with a solution to a different problem.

I have mapped "Copy, paste" (yank, put) from the system clipboard to "Ctrl-Shift-C, Ctrl-Shift-V" (Ctrl-C, Ctrl-V if caps lock is on). This can be used in place of y with the same effect.

If I use the system buffer for copy it does not get overwritten when I paste.

I added this to my .vimrc

vnoremap <C-V> "*p 
vnoremap <C-C> "*y

As a bonus it will give you easy access to the system clipboard.

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53
scorch855
  • 302
  • 1
  • 9