How to copy and paste text in Vim
yyyiwpPIn Vim, copying is called yanking and pasting is called putting. yy copies the current line. p puts it on a new line below; 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: copying also fills register 0, and deleting does not touch it.
- To delete without losing your copy, delete into the black hole register: "_dd.
- A count puts several copies: 3p.
- Vim's registers are its own. Copying with y does not reach your system clipboard unless your Vim is set up for it.