10

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. This is frustrating.

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

My expectation is that the clipboard persists after the first paste.

0

6 Answers 6

8

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
8
  • 1
    Can you pleas explain what 'pgv"'.v:register.'y>'` is doing ?
    – ng.newbie
    Commented May 12, 2020 at 9:53
  • @ng.newbie I did explain it in english right after the it. Or you want more detailed explanation?
    – Maxim Kim
    Commented May 12, 2020 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
    Commented May 12, 2020 at 10:05
  • If you could break done the command it would be nice, otherwise I will have to create a separate thread just for the explanation.
    – ng.newbie
    Commented May 12, 2020 at 10:06
  • 2
    Sure, but it is always good to just :h gv, :h v:register, :h "<tab> etc
    – Maxim Kim
    Commented May 12, 2020 at 10:13
10

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

4

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.

1
  • 2
    You don’t need to use a named register for this method: you can just use the yank register "0
    – Rich
    Commented May 11, 2020 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.

0

If you're using the VS Code vim plugin, you can add these in the settings.json

  "vim.normalModeKeyBindingsNonRecursive": [
    {
      "before": ["p"],
      "after": ["\"", "0", "p"]
    },
    {
      "before": ["P"],
      "after": ["\"", "0", "P"]
    }
  }
0

This worked for me in neovim lua.

When using LazyVim, it did not work in keymaps.lua, because I have enabled lazyvim.plugins.extras.coding.yanky. So without this plugin, this should work:

vim.api.nvim_set_keymap('v', 'p', 'P')

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.