Registers and Clipboard
You’ve already used copy and paste (y and p) without knowing where the
copied text actually goes. This chapter explains that, and how to get
things in and out of your system clipboard so Vim can talk to the rest of
your computer.
Where yanked text lives
Every time you yank or delete text, it goes into a register, Vim’s version
of a clipboard slot. By default, y and d both use the unnamed
register, written as ". That’s why p after a dd pastes the line you
just deleted: delete and yank share the same default storage.
Named registers
You’re not limited to one slot. Prefix a yank or delete with " followed
by a letter to store it in a named register instead of overwriting the
default one. "ayy yanks the current line into register a. "ap pastes
from register a. This matters once you’re juggling more than one thing
at a time, copy one line into "a, copy another into "b, and paste
either one whenever you need it without them overwriting each other.
The system clipboard
Registers " and the named ones (a through z) are internal to Vim.
They don’t talk to your operating system’s clipboard by default, so
Ctrl-c in your browser and y in Vim land in different places. To
bridge them, Vim has two special registers:
"+is the system clipboard (the oneCmd-c/Ctrl-cuse elsewhere)"*is the “selection” clipboard on some Linux setups (less relevant on macOS and Windows)
To yank a line into your system clipboard: "+yy. To paste from your
system clipboard into Vim: "+p. This is the pair to memorize if you want
to move text between Vim and other applications.
Neovim note: Neovim has the same register system, and on most modern
installs "+ works out of the box. In classic Vim, clipboard support
depends on how it was compiled, run vim --version and look for +clipboard
(with a plus sign, not a minus) to confirm yours has it.
Try it
Open a scratch file with several lines of distinct text and practice:
- Delete a line with
dd, then paste it back withp. Notice the deleted text was sitting in the default register the whole time. - Yank one line into register
awith"ayy, then yank a different line into registerbwith"byy. Paste each one back with"apand"bp, in either order, and confirm they didn’t overwrite each other. - Yank a line into your system clipboard with
"+yy, then paste it into another application (a browser address bar, a notes app, anything) with your normal paste shortcut. - Copy some text from another application, then paste it into Vim with
"+p.