Learn Practice Daily golf Reference

How to copy and paste in Vim

In Normal mode, yy copies the line and yiw the word under the cursor; move to where it goes and press p to paste after the cursor or P before it. To cut, delete instead: dd or dw, then p.

Try it here

Copy a word, cut a line

First copy: the cursor is on text. yiw yanks the word, j goes down, f) finds the bracket, and P puts the word before it. Then cut: the stray import at the bottom goes to the top, with G, dd, gg and P.

1 of 2Copy text into JSON.parse()
3import { readFile } from 'node:fs/promises';
2
1export async function loadConfig(path) {
4 const text = await readFile(path, 'utf8');
1 return JSON.parse();
2}
3import { join } from 'node:path';
NORMALLoading the editor…1/20.0s4:9

This editor needs a keyboard. Read on, and play it at your computer.

KeysWhat it does
yyCopy the whole line.
3yyCopy three lines, starting with this one.
ywCopy from the cursor to the start of the next word.
yiwCopy the word under the cursor, wherever in the word you are.
y$Copy to the end of the line.
yapCopy the paragraph, and the blank line after it.
p / PPaste after / before the cursor. Whole lines go below / above the current line.
3pPaste three copies.
dd / dw / xCut the line / the word / the character under the cursor.
v or V, then y or dCopy or cut exactly what you selected (characters with v, whole lines with V).
gp / gPPaste like p / P, but leave the cursor just after the pasted text.

Why it is called yank

Vim has its own words for it: copying is yanking and pasting is putting. With c already taken by change, copy got y, for yank. The rest follows the same pattern as every other Vim command: an operator, then what it acts on. y takes the same motions and text objects as d and c, so yw copies a word just as dw deletes one, and yi( copies what's inside the brackets.

Copying never changes the screen, and the cursor stays put (or moves to the start of what you copied). The text waits in a register until you paste it.

Cut is delete

There's no separate cut command, because every delete is a cut. dd, dw, x, and d in Visual mode all put what they removed in the same register that y fills, so p pastes it back wherever you like. That's how xp swaps two letters and ddp swaps two lines.

What you copied as whole lines pastes as whole lines: p opens a new line below the current one, whatever column the cursor is in. A word or part of a line pastes into the line, right after the cursor.

Not the system clipboard

By default, y and p use Vim's own register, not the clipboard other programs share, so text yanked in Vim won't paste into your browser with Cmd-V or Ctrl-V. "+y copies to the system clipboard and "+p pastes from it, when your Vim is built with clipboard support.