Registers

Think of registers as labeled drawers in a desk. Most editors give you a single clipboard — one drawer that gets overwritten every time you copy something new. Vim gives you an entire filing cabinet: drawers that fill automatically as you work, drawers you label yourself for safekeeping, read-only drawers that always hold useful metadata, and even a special drawer connected to your system clipboard so you can pass text to other applications. Once you learn which drawer holds what, you will never again lose a piece of text you just deleted because you copied something else on top of it.

Most Vim users underuse registers, sticking to the single-clipboard habits they carried over from other editors. This chapter walks through each register type so you can start putting the full filing cabinet to work.

See :h registers for the full documentation, and the excellent article "Vim registers: The basics and beyond"arrow-up-right for a complete guide.

What you'll learn in this chapter:

  • Understand the different types of registers Vim provides and when each one is used automatically

  • Use named registers ("a-"z) to store and retrieve multiple pieces of text simultaneously

  • Copy and paste between Vim and external applications using the clipboard register ("+)

  • Leverage the expression register ("=) for inline calculations and dynamic content insertion

  • Use the black hole register ("_) to delete text without disturbing your yank history

The Unnamed Register ""

The unnamed register (also called the default register) is filled automatically by d, c, s, x, and y commands. It always points to the last used register.

Practical use: Toggling characters with xpx cuts a character to the default register, p pastes it after the cursor. For toggling lines, ddp swaps the current line with the next.

Note: When you copy or delete characters within a line, the operation is characterwise. When you delete an entire line, it's linewise. Vim remembers this and pastes accordingly (see Modes, Navigation & Basic Editing for more on yanking basics). See :h linewise.

The Read-Only Registers ":, "%, ".

Beyond the unnamed register, Vim maintains several read-only registers that quietly record what you have been doing. You never write to these yourself — Vim fills them in the background — but being able to pull from them at a moment's notice is surprisingly handy.

Register
Content
How to access in Insert mode

":

Last executed command

Ctrl+r:

"%

Current filename

Ctrl+r%

".

Last inserted text

Ctrl+r.

Copy last command to clipboard: :let @+=@:

Insert filename in text: In Insert mode, Ctrl+r% inserts the current filename.

You can also create an insert abbreviation for the filename:

Tip: To quickly see the filename: press Ctrl+g in Normal mode, or run :file. Set :set title to always show the filename in the title bar.

The Expression Register "=

The registers we have seen so far store static text. The expression register is different: it does not store anything permanently. Instead, it evaluates a Vimscript expression on the fly and inserts the result. It is Vim's built-in calculator and scripting shortcut, rolled into one.

The expression register lets you evaluate Vimscript expressions and insert the result. In Insert mode, press Ctrl+r= and you'll see an = prompt at the bottom.

Expression register

Basic math: If you have 345*3= in your file, position cursor after =, enter Insert mode, and type Ctrl+r=345*3Enter.

In substitutions: Calculate ages from birth years:

Explanation:

  • \d\+$ — one or more digits at the end of the line

  • \= — start expression in the replacement

  • strftime("%Y") — current year

  • submatch(0) — the matched digits

Doing math inline:

The Clipboard Register "+

If you are coming from VS Code, Sublime Text, or any other modern editor, this is the register you have been looking for. Every editor in the world lets you Ctrl+C and Ctrl+V between applications, and Vim can do the same — it just does not do it by default. Vim's internal registers and the operating system clipboard are separate worlds, and the "+ register is the bridge between them. Understanding this one register eliminates the most common frustration new Vim users face: "I copied something in Vim but I can't paste it in my browser."

The clipboard register connects Vim to the system clipboard. This is how you copy/paste between Vim and other applications.

Pasting from clipboard

Tip: Ctrl+r Ctrl+p + pastes from the clipboard while respecting the current indentation level. This is especially useful when pasting code blocks.

Copying to clipboard

Pasting clipboard multiple times

Note: The silent! option makes Vim run the loop with no output, which is faster.

Neovim: Neovim has better clipboard integration. Set vim.opt.clipboard = 'unnamedplus' in your init.lua to always use the system clipboard.

The Search Register "/

Vim also keeps track of your searches. Every search creates an entry in the search history. The last search is stored in the Search Register. Type q/ in Normal mode to open the search history window.

The Command-Line History q:

Every command you type feeds this register. Type q: in Normal mode to open the command history window. Press Esc to close it.

Tip: In Insert mode, Ctrl+r: inserts your last command. You can also copy it to the clipboard: :let @+=@:

Named Registers "[a-z]

All the registers above are managed by Vim automatically. Named registers are yours — the drawers you label and fill yourself. You can use 26 named registers (a through z) to store different pieces of text. The uppercase version appends to the register instead of replacing. Named registers are also where macros are stored — when you record a macro with qa, the keystrokes are saved to register a, and you can even edit them directly (see Macros).

Tip: vim-highlightedyankarrow-up-right makes the yanked region visible. In Neovim, this is built-in — add vim.highlight.on_yank() in an autocommand.

Numbered Registers "[0-9]

While named registers require you to be deliberate, numbered registers work silently in the background, giving you a short-term history of your recent yanks and deletions. Think of them as an automatic undo history for your clipboard.

  • Register "0 — contains text from the most recent yank

  • Register "1 — contains text from the most recent delete/change

  • Registers "2 through "9 — contain progressively older deletions

Each successive deletion shifts the previous content down: "1"2"3 ... → "9 (lost).

Tip: After deleting something, if you yank something new, the deleted text is still in "1. Use "1p to paste it.

The Black Hole Register "_

Sometimes you want to throw text away — truly discard it — without disturbing the text you have carefully yanked into another register. That is what the black hole register is for. Anything sent here vanishes completely, like /dev/null for your editor.

When you want to delete text without affecting any register, use the black hole register:

The Alternate Buffer Register "#

The # register contains the name of the alternate buffer. Press Ctrl+6 to toggle between the current and alternate buffer.

Tip: If you want to keep seeing the current file while opening another: :sp newfile (horizontal split) or :vsp newfile (vertical split).

Clearing File Content

To delete all lines in a file, use:

The % range means "all lines", and d deletes them. Using _ sends deleted text to the black hole register so your other registers aren't affected.

Quick Reference: Manipulating Registers

Action
Command

See all registers

:registers or :reg

See specific register

:reg a

Paste register in Normal mode

"ap

Paste register in Insert mode

Ctrl+r then register letter

Set register via command line

:let @a = 'text'

Clear a register

:let @a = ''

Append to register

"Ayy (uppercase letter)

Copy register to clipboard

:let @+ = @a

When to Use What

With so many register types, it is natural to wonder which ones actually matter day to day. Here is a practical guide.

For most editing, you do not need to think about registers at all. The unnamed register ("") and numbered registers ("0-"9) handle things automatically. When you yank text, it lands in "0. When you delete text, it goes into "1 and the older deletions shift down. This means you can always recover a recent deletion with "1p, "2p, and so on, even if you have yanked something else in the meantime.

When you need to move text between Vim and another application, reach for the clipboard register ("+). This is the one you will use to copy a snippet into a Slack message, a browser, or a different terminal. If you find yourself doing this constantly, consider setting clipboard=unnamedplus (Neovim) so that every yank and paste automatically uses the system clipboard.

Named registers ("a-"z) earn their keep during complex refactoring sessions. If you are juggling multiple pieces of text — say, moving a function signature to one place and its documentation to another — stash each piece in a named register and paste them independently. Outside of these multi-step operations, you rarely need them.

The black hole register ("_) is essential whenever you want to delete text without overwriting what you just yanked. The classic scenario: you yank a line, move to another location, and want to delete the existing text and replace it with what you yanked. Use "_d to delete the old text, then p to paste your yank — the unnamed register is untouched.

The read-only registers and the expression register are situational but powerful. Insert the current filename with "%, replay your last command with ":, or do quick arithmetic with "=. You may not use them every session, but when you need them, they save a surprising amount of time.

Start with the unnamed register, the clipboard register, and the black hole register. Those three will cover the vast majority of your daily work. As your editing grows more complex, the rest of the filing cabinet will be waiting.

Summary

This chapter covered Vim's register system — from the unnamed register that silently captures every yank and delete, through named registers you control yourself, to special-purpose registers like the clipboard, expression, and black hole registers. Understanding registers transforms the way you move text around, eliminates the frustration of accidentally overwriting your clipboard, and opens the door to advanced workflows like macro editing and programmatic content insertion.

Key takeaways:

  • The unnamed register ("") automatically captures text from d, c, s, x, and y operations, while numbered registers ("0-"9) maintain a history of recent yanks and deletions

  • Named registers ("a-"z) let you store multiple pieces of text independently; uppercase versions ("A-"Z) append rather than replace

  • The clipboard register ("+) bridges Vim and the system clipboard — use "+y to copy out and "+p to paste in

  • The black hole register ("_) discards text permanently, preventing it from overwriting your other registers

  • The expression register ("=) evaluates Vimscript expressions on the fly, acting as a built-in calculator and scripting tool

Exercises

  1. Explore your registers — Open a file, yank a line with yy, delete a different line with dd, then type :reg to inspect all registers. Identify where each piece of text ended up.

  2. Use named registers — Yank three different lines into registers a, b, and c, then paste them in reverse order at the end of the file.

  3. The black hole register workflow — Yank a line, then move to a different line and replace it with the yanked content without losing it. Use the black hole register to delete the target line first.

  4. Clipboard integration — Copy a paragraph to the system clipboard, switch to another application (or another Vim buffer), and paste it. Then copy text from an external source and paste it into Vim.

  5. Expression register calculation — Use the expression register to insert the result of 256 * 16 into your buffer without leaving Vim. Then use a substitution with \= to replace all numbers in a line with their doubled values.

Last updated

Was this helpful?