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

China☆狼群 提交于 2019-12-05 17:43:18

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.

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.

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