Vim Registers
Vim registers are a powerful feature that allow you to store text, yanked or deleted, for later use. They act as a clipboard, but with multiple slots, providing flexibility beyond a simple copy-paste mechanism. Understanding how to use registers effectively can drastically improve your editing efficiency in Vim.
There are several types of registers, each designed for a specific purpose:
Unnamed Register (“”): The default register. Most yank, delete, and change operations use this register unless you specify another.
Numbered Registers (“0” - “9”): Ten registers that store the last ten yanked or deleted pieces of text. Register “0” holds the text of the most recent yank, while “1” through “9” hold previously yanked/deleted text. Deleting overwrites the oldest entry.
Named Registers (“a” - “z”): Twenty-six registers that you can explicitly name for storing text. This allows you to save specific pieces of text for later use without worrying about them being overwritten by subsequent yanks or deletes.
Read-only Registers:
- ”:” register: Stores the last command executed.
- ”.” register: Stores the last inserted text.
- ”%” register: Contains the current filename.
- ”#” register: Contains the alternate filename (previous file edited).
- ”/” register: Contains the last search pattern.
System Clipboard Register (“+” and “*”): These registers interact with the system clipboard. The “+” register usually represents the system clipboard selection, while the “*” register often represents the clipboard used for copy-pasting. Availability and behavior can depend on your system and Vim configuration.
Here’s how you use them:
Yanking and Deleting into Registers:
To yank or delete into a specific register, prefix the command with "register_name. For example:
"aYyanks the current line into register “a”."f5dddeletes 5 lines and puts them in register “f”.
Pasting from Registers:
To paste from a register, use "register_namep (for putting after the cursor) or "register_nameP (for putting before the cursor). For example:
"appastes the content of register “a” after the cursor."fPpastes the content of register “f” before the cursor.
Viewing Register Contents:
You can see the contents of a register using the :registers command (shows all registers) or :registers register_name (shows a specific register).
Example - JavaScript Refactoring:
Imagine you have the following JavaScript function:
function greet(name) {
console.log("Hello, " + name + "!");
}
greet("World");
You want to refactor it to use a template literal:
- Yank the greeting message into register “g”:
"gci"Hello, ${name}! - Delete the existing string:
ci“console.log(`); - Move the cursor inside the backticks and paste from register “g”:
"gp
Now the code looks like:
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet("World");
By strategically using registers, you can efficiently move and manipulate text within your code, significantly speeding up your workflow. Experiment with different registers and find what best suits your editing style. Mastering Vim registers is a key step in becoming a Vim power user.