Vim - Registers
Vim - Understanding Registers
Section titled “Vim - Understanding Registers”Vim provides a set of registers, which are essentially storage locations for text. Think of them as multiple, named clipboards. This feature is incredibly useful for yanking (copying), deleting, and pasting text, especially when working with multiple pieces of text or across different files.
- Yanking (copying) text into a register
- Pasting text from a register
- Listing available registers and their contents
- Exploring different types of registers
Yanking Text into a Register
Section titled “Yanking Text into a Register”To yank text into a specific register, precede the yank command (y) with " (double quote) followed by the register’s name (a letter, number, or symbol). If you omit the register name, Vim uses the “unnamed” register.
Syntax: "<register-name><yank-command>
For example, to yank the current line into register a:
"ayyTo yank the current word into register b:
"bywSimilarly, deleting text with d or changing text with c also populates registers. For example, "add deletes the current line and stores it in register a.
Pasting Text from a Register
Section titled “Pasting Text from a Register”To paste text from a specific register, precede the paste command (p or P) with " and the register’s name.
Syntax: "<register-name><paste-command>
For instance, to paste the content of register a after the cursor:
"apTo paste the content of register b before the cursor:
"bPList Available Registers
Section titled “List Available Registers”To view the contents of all registers, use the command:
:registersOr its shorter alias:
:regYou can also display specific registers, for example, to see registers a, b, and 0:
:reg a b 0This command is very useful for seeing what text you have available for pasting.
Register Types
Section titled “Register Types”Vim supports several types of registers, each with specific behaviors:
The Unnamed Register (")
Section titled “The Unnamed Register (")”Denoted by "" (or simply by not specifying a register). This is the default register. Any text yanked with y or deleted with d, c, s, x without specifying a register goes here. p or P without a register name pastes from this register.
Named Registers (a to z)
Section titled “Named Registers (a to z)”There are 26 named registers, "a through "z. When you use a lowercase letter (e.g., "ayy), the register’s content is overwritten. When you use an uppercase letter (e.g., "Ayy), the yanked text is appended to the existing content of that register (e.g., if register a contains “hello”, "Ayy with the line ” world” will make register a contain “hello\n world”). These are great for collecting multiple pieces of text.
Numbered Registers (0 to 9)
Section titled “Numbered Registers (0 to 9)”These are automatically managed by Vim:
- Register
"0(yank register): Contains the text from the most recenty(yank) command. - Registers
"1to"9(delete/change history):"1contains the text from the most recent delete or change command (unless it was very small, less than one line, and didn’t involve linewise deletion)."2contains the text from the second most recent deletion, and so on, up to"9. This forms a history of larger deletions.
This is useful for recovering recently deleted text. For example, if you delete a line with dd, then another with dd, you can paste the first deleted line with "2p and the second with "1p.
Read-only Registers
Section titled “Read-only Registers”These registers provide information and cannot be written to directly (though some are updated automatically):
| Register | Description |
|---|---|
"% | Name of the current file. |
"# | Name of the alternate file for the current window. |
": | The most recently executed Ex command (command-line command). |
". | The last inserted text. (Useful with Ctrl-R . in insert mode or ".p in normal mode) |
"/ | The last search pattern. |
Special Registers
Section titled “Special Registers”- Black Hole Register (
"_): Any text yanked or deleted into this register is discarded. Useful if you want to delete text without affecting the unnamed or numbered registers (e.g.,"_dd). - System Clipboard Registers (
"*and"+): If Vim is compiled with clipboard support,"*usually refers to the X11 selection (primary clipboard, often used with middle-mouse click), and"+refers to the system-wide clipboard (used with Ctrl+C/Ctrl+V). To copy the current line to the system clipboard:"+yy. To paste from system clipboard:"+p. This is extremely useful for interacting with other applications. See:help clipboard. - Expression Register (
"=): Allows you to evaluate an expression and use its result. When you type"=in Normal mode (e.g., for pasting) orCtrl-R =in Insert mode, Vim prompts you to enter an expression. For example,Ctrl-R =strftime('%Y-%m-%d')<CR>in Insert mode will insert the current date. See:help expression-register.