Learn Vim / How to Copy an Entire File in Vim

How to Copy an Entire File in Vim

The answerPress gg then yG — go to the top, yank to the last line. Or run :%y.

Try it on a real buffer

Copy the entire file: gg to jump to the top, then yG yanks from here to the last line. (Or try :%y — or ggVGy with visual mode.) The file must stay unchanged.

# dotfiles worth stealing
set relativenumber
set clipboard=unnamedplus
remap caps lock to escape

Canonical solution: gg yG (or :%y) · par: 4 keystrokes (vimgolf rules — every keypress counts).

Why it works

Operators combine with any motion: yG = yank to end of file, y5j = yank five lines down. The ex flavor :%y yanks every line without moving the cursor at all.

Variations

KeysWhat it does
ggyGyank the whole file
:%ysame, without moving the cursor
ggVGysame, via visual mode (you see the selection)
:%y +yank the whole file to the system clipboard

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

Practice free — no signup →

Keep going