Learn Vim / How to Copy a Line in Vim

How to Copy a Line in Vim

The answerPress yy to yank (copy) the current line, then p to paste it below the cursor.

Try it on a real buffer

Copy line 5 into vim's clipboard (a register): jump there with 5G, then yy yanks the whole line. The file must stay unchanged.

1. talk is cheap, show me the keystrokes
2. the mouse is a rumor
3. modes are not a bug
4. speed is a side effect of precision
5. yank responsibly
6. registers remember what you forget
7. macros do the boring parts
8. :q is goodbye, not defeat

Canonical solution: 5G yy · par: 4 keystrokes (vimgolf rules — every keypress counts).

Why it works

y is the yank (copy) operator; yy yanks the current line. Yanked text lands in registers — vim's clipboards. Check your registers anytime with :registers.

Variations

KeysWhat it does
yycopy the current line
3yycopy three lines
5G yyjump to line 5, then copy it
:5ycopy line 5 without moving
p / Ppaste below / above

Reading about keystrokes doesn't build keystrokes. Try this in a real vim buffer right now — the “Copy line five” mission takes about a minute.

Practice free — no signup →

Keep going