2

I am on Neovim.

This is really annoying and weird.

When I make a selection in Visual Mode and copy, then select a text and then paste, this paste only works once. For the next time, I again have to go to the original source and then do the copy operation all over and then paste.

What I am doing:

  1. Press v to go into Visual Mode.
  2. Select the text I want to copy with my arrow keys.
  3. Press y to copy.
  4. Press Esc to go into Normal Mode.
  5. Navigate the area of text I want to paste, with my arrow keys
  6. Press v for Visual Mode and select the text I want to change.
  7. Press p to paste. This works fine.
  8. Press Esc to go into Normal Mode.
  9. If I select another piece of text and press p nothing happens. I have to redo the copy process all over. Why ???

This is incredibly weird and frustrating.

Why is this happening ? Is the clipboard overwritten in Visual Mode each time?

How do I get "sane" behavior? That is make the clipboard persist after the first paste.

2

Is the clipboard overwritten in Visual Mode each time?

Yes it is.

How do I get "sane" behavior? That is make the clipboard persist after the first paste.

To get "sane" behavior you can remap p and P with:

" now it is possible to paste many times over selected text
xnoremap <expr> p 'pgv"'.v:register.'y`>'
xnoremap <expr> P 'Pgv"'.v:register.'y`>'

which is paste, reselect pasted(previously selected text actually, but you already pasted over it) and yank it again (with regards to register used)

UPD

  1. xnoremap is to remap in visual mode
  2. <expr> expression should be on the mapped (right side)
  3. 'pgv"'.v:register.'y`>' expression that concatenates vim normal commands and v:register:
    • p to paste
    • gv to reselect previous visual selection
    • " is to use register that should follow
    • v:register is the register used for our mapped command
    • y is to yank reselected visual selection to v:register
    • `> is to go to the end of pasted value
| improve this answer | |
  • Can you pleas explain what 'pgv"'.v:register.'y>'` is doing ? – ng.newbie May 12 at 9:53
  • @ng.newbie I did explain it in english right after the it. Or you want more detailed explanation? – Maxim Kim May 12 at 10:03
  • What does gv do ? What is the the use of " I don;t understand the meaning of .v:register.'y. Yes I want a more detailed explanation. – ng.newbie May 12 at 10:05
  • 1
    Sure, but it is always good to just :h gv, :h v:register, :h "<tab> etc – Maxim Kim May 12 at 10:13
  • 1
    xnoremap is for Visual mode only. See Why is vmap used for both Visual and Select mode? for details. – filbranden May 12 at 14:27
2

In step 7 you delete the text you selected in step 6, so it is moved to the unnamed register ("clipboard").

This the normal behavior of Vi(m).

If you want to copy something multiple times, you should put it into a named register, e.g. into register 'a'.

In step 3 you would use "ay and in step 7 you would use "ap.

The unnamed register still changes in step 7, but the register a can be reused.


Different approach: As Rich pointed out in a comment, it is easier to use the register "0. This register contains the text from the most recent yank command. In this case you would use "0p in steps 9 and following. (You could also use it in step 7.)


See :h registers and following

BTW: Step 4 and 8 are not needed as you are already in normal mode.

| improve this answer | |
  • 2
    You don’t need to use a named register for this method: you can just use the yank register "0 – Rich May 11 at 23:21
2

There's no single "clipboard" in Vim. These things are called registers. Make sure you read the docs about them, it's essential.

This behaviour is documented under :h v_p (as you "put" while being in "visual" mode). "Visual put" consists of two actions: a) put new text from register, and b) delete the old text into a register. Here deleted text is put either into register named "1" (and then the old text from "1" goes into "2", and the old "2" into "3" and so on upto "9"), or into the register "-" ("small deletion" register used if text is lesser than one line).

Now, "put" command by default uses "unnamed register", which in fact "the register last used by any yank/delete/put operation". So the first time, it's register "0" (used by your "yank"). But the next time it's the register "1" (or "-") used by last "visual put". So every time, except the first one, you're trying to "put" from the wrong register, while your text is still hold in the register "0".

To overwrite the register used by a command you have to prepend it with " followed by register name.

| improve this answer | |
1

Use "0p to paste in Visual mode

Note that the numbered register "0 can be really useful here, since a default yank also goes to the "0 register, but the text replaced with a put in visual mode only goes to the unnamed register and doesn't modify "0.

See :help v_p:

The previously selected text is put in the unnamed register. If you want to put the same text into a Visual selection several times you need to use 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, the unnamed register will be changed each time.

(From my answer to a similar question on StackOverflow.)

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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