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

为君一笑 提交于 2019-12-10 09:22:41

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/25267062/after-pasting-a-yanked-line-in-vim-why-cant-i-paste-it-again

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!