Vim Yank and Paste — y, p, P, and Paste Registers
Master copying and pasting in Vim with yank (y), put (p/P), and the numbered/named registers. Understand how Vim's yank-delete-put system differs from traditional copy-paste.
Detailed Explanation
Vim's Copy-Paste System
In Vim terminology, "yank" means copy and "put" means paste. The key insight is that d (delete) also copies text to a register, so deleted text can be pasted.
Yank Commands
| Command | Action |
|---|---|
yy or Y |
Yank (copy) entire line |
yw |
Yank to start of next word |
ye |
Yank to end of word |
y$ |
Yank to end of line |
y0 |
Yank to beginning of line |
yiw |
Yank inner word |
yi" |
Yank inside double quotes |
yip |
Yank inner paragraph |
3yy |
Yank 3 lines |
Put (Paste) Commands
| Command | Action |
|---|---|
p |
Put after cursor (or below current line for line-wise) |
P |
Put before cursor (or above current line for line-wise) |
gp |
Like p, but leave cursor after pasted text |
gP |
Like P, but leave cursor after pasted text |
The "0 Register Trick
When you yank text, it goes into the "0 register. When you delete text, it goes into "1 through "9 (a delete history stack) and the unnamed register "".
This means after yanking text and then deleting something:
ppastes the deleted text (unnamed register)"0ppastes the yanked text (yank register)
This solves the common problem of yanking text, deleting what you want to replace, and then losing the yanked text.
System Clipboard
| Command | Action |
|---|---|
"+y{motion} |
Yank to system clipboard |
"+p |
Paste from system clipboard |
"*y{motion} |
Yank to primary selection (Linux X11) |
Use Case
You need to copy, move, and rearrange text efficiently in Vim, including working with the system clipboard and understanding how Vim's register system affects pasting.