After pasting a yanked line in Vim, why can't I paste it again?
Asked Answered
I

2

9

This question was probably answered before, but I tried searching and could not find the answer anywhere.

I am somewhat new to Vim and I am having the following issue. Once I yank a line and paste it, i cannot paste it again. For example, say in Word environment you would copy a text, paste it and then you can paste it again further. But in Vim, once I have pasted it and then try pasting again (p), it pastes the text I pasted the yanked line over.

So for example, I yanked the line "This line is yanked" onto "I don't want this line" and so "This line is yanked" takes place over "I don't want this line". If I click p further on again, I won't get "This line is yanked" pasted but will get "I don't want this line".

Is there a way I can paste the same yanked line over again without going back and yanking it again?

Innis answered 12/8, 2014 at 14:34 Comment(0)
J
14

This is because of vim's registers. When you paste a yanked line over another line, the line you just deleted (by pasting over it) takes up the place of the yanked line in the default register (which stores yanked lines). This is to make switching lines easy. Yank one, paste over the other and go back and paste again. However, to keep your yanked line you can specify a register, so instead of using y you can use "ay and this will yank your line into register a. Now to paste you can use "ap and this will paste the contents of register a, which will not get overwritten.

As a bonus "+y or "*y and "+p or "*p paste from the system clipboard (other applications' copy paste) if it is enabled in vim (which it is on most systems).

EDIT: As mentioned in the comments, when you use the yank command, the yanked text not only goes into the default register but also to the 0 register (which won't get overwritten when you paste over something else). This means that you can normally yank using y and then paste it with "0p and it won't get overwritten by anything you paste over.

Jujube answered 12/8, 2014 at 14:39 Comment(2)
Everything in this answer is true. I just want to add that yanking doesn't only go into the default register -- it also goes into the 0 register, which always holds the most recently yanked text; it doesn't get overwritten by deletes or changes the way that the default register does. This means that if you want to paste something again, even after performing deletes, you can just "0p, without ever needing to go yank the text again or manually yank into a named register.Ardith
Thank you, your answer explains it well!Innis
F
12

From my .vimrc:

"Paste in visual mode without copying
xnoremap p pgvy

Explanation:

xnoremap - remap only in visual mode

p - Paste

gv - Reselect last selection (not the one that you currently on, but the original)

y - copy it (last selection)

Within this mapping you can paste over visually selected lines over and over.

Fang answered 13/8, 2014 at 9:8 Comment(1)
This is great! For completeness, one can also add xnoremap P Pgvy and it will work with PGamesome

© 2022 - 2024 — McMap. All rights reserved.