How to copy and paste text in Vim
yyyiwpPIn Vim you copy with y, which is called yanking, and paste with p, which is called putting. yy copies the current line. p puts it on a new line below and P puts it above.
y works with any motion or text object. yiw copies a word, and p then puts it after the cursor. Deleting also keeps the text, so dd then p moves a line.
Worked example
Make a second copy of the first line.
A line copied with yy is put as a whole line, below the cursor's line, wherever the cursor is on it.
Commands
- yy
- Copy the line.
- p
- Put after the cursor, or below the line for a line copy.
- P
- Put before the cursor, or above the line.
- yiw
- Copy the word under the cursor.
- ddp
- Swap a line with the one below it.
- xp
- Swap two characters.
Exceptions
- A delete replaces what you copied. "0p still puts your last copy, because copying also fills register 0 and deleting doesn't touch it.
- To delete without losing your copy, use "_dd. It deletes into the black hole register.
- Add a count to put several copies, like 3p.
- Vim's registers are its own. Copying with y does not reach your system clipboard unless your Vim is set up for it.