How do I yank the entire file contents into a register from the command line?
Answer
:%y
Explanation
:%y yanks the entire file into the unnamed register in a single Ex command. The % range covers all lines (equivalent to 1,$), and :y is the yank command. You can also specify a named register to target the yank precisely.
How it works
%— the range for all lines in the file:y— the yank (copy) Ex command:%y— yank all lines into the unnamed register":%y a— yank all lines into named registera:%y +— yank all lines into the system clipboard register- After yanking, paste with
p(below) orP(above) in Normal mode, or<C-r>"in Insert mode
Example
Yank the whole file to the system clipboard:
:%y +
Now paste it anywhere outside Vim.
Yank the whole file to register a for later use:
:%y a
Then in another buffer: "ap to paste it.
Tips
- The Normal-mode equivalent is
ggVGy(go to top, select all, yank) —:%yis faster to type :%ddeletes all lines (clears the buffer);:%yfollowed by:%dmoves the file to a register:%yis especially useful in scripts and mappings where you want to capture the full buffer programmatically- Combine with
:g::%g/TODO/y Ayanks all TODO lines into registera(appending), collecting them without deleting