Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

paste

yank

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?

like image 255
rachkov91 Avatar asked Aug 12 '14 14:08

rachkov91


2 Answers

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.

like image 114
Zach Avatar answered Oct 01 '22 10:10

Zach


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.

like image 43
user3891746 Avatar answered Oct 01 '22 09:10

user3891746