vimtricks.wiki Concise Vim tricks, one at a time.

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 register a
  • :%y + — yank all lines into the system clipboard register
  • After yanking, paste with p (below) or P (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) — :%y is faster to type
  • :%d deletes all lines (clears the buffer); :%y followed by :%d moves the file to a register
  • :%y is especially useful in scripts and mappings where you want to capture the full buffer programmatically
  • Combine with :g: :%g/TODO/y A yanks all TODO lines into register a (appending), collecting them without deleting

Next

How do I run a search and replace only within a visually selected region?