Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 36 ยท Registers, Macros and Search

Vim Registers: Yank, Delete History and Clipboard

Understand Vim registers: the unnamed register, yank register 0, numbered delete history, named a-z registers, the system clipboard and the black hole.

Most editors have one clipboard. Vim has dozens, called registers, and they explain two things beginners find odd: why a delete can overwrite what you just copied, and how experts keep several snippets alive at once. Learn the rules once and registers become one of the nicest parts of Vim.

Using a register

Type " followed by the register name before an operator or put command:

  • "ayy yanks the current line into register a.
  • "ap pastes register a.
  • "Ayy (capital) appends to register a instead of replacing it.
  • :registers (or :reg) lists everything; :reg a b shows only those.
  • In Insert or Command-line mode, Ctrl-r a pastes register a.

If you give no register, Vim uses the unnamed register "", which is what plain p pastes.

The register types

Register What it holds
"" unnamed: the last delete or yank
"0 the last yank only
"1 ... "9 history of larger deletes, newest in 1
"- small deletes (less than a line)
"a ... "z named registers you choose
"A ... "Z append to the matching named register
"_ black hole: discards text
"+ and "* system clipboard (and X11 primary selection)
". last inserted text
": last command-line command
"/ last search pattern
"% current file name
"# alternate file name
"= expression register

The classic confusion

You yank a word (yiw), move to another word and delete it (diw), then press p expecting the yanked word. Instead the deleted word appears. Deletes also write to the unnamed register. The fixes:

  1. Paste from register 0 with "0p: it always holds the last yank.
  2. Delete into the black hole with "_diw so the unnamed register is untouched.
  3. Use Visual mode paste: select the target, press p. Vim replaces the selection with the unnamed register, but note the replaced text goes into the unnamed register, so repeated pastes use the replaced text unless you use "0p or P (in recent Vim 9 and Neovim, Visual P does not overwrite the register).

Memorize: yank lives in 0, delete lives in the unnamed register and in 1-9.

The numbered history

Deletes of one line or more shift through "1 to "9: each new delete goes to "1, the old "1 moves to "2, and so on. That is a built-in multi-level clipboard. If you deleted something several steps ago, "3p recovers it. A trick: after "1p press u. repeatedly; . increments the register number each time, so you walk back through your delete history.

JS Register model (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output confirms the confusion and its fix: after yank then delete, the unnamed register holds the deleted text, while "0 still holds the yank.

Named registers and appending

Named registers are your scratch memory. Collect several lines into one register: "ayy on the first, then "Ayy on others, then "ap to paste them together. Use "ayiw, "byiw to hold two words and paste each with "ap and "bp. Named registers last for the session, and uppercase append saves you from clearing and re-yanking.

The system clipboard

"+ is the system clipboard on most systems and "* the primary selection on X11 (selection) or the same clipboard on Windows and macOS.

  • Copy to the system clipboard: "+yy, or in Visual mode "+y.
  • Paste from it: "+p, or Ctrl-r + in Insert mode.
  • Check with :echo has('clipboard'). A 0 means the build lacks clipboard support (see installation).

To make every yank use the clipboard, add set clipboard=unnamedplus to your config (chapter 6). Some people prefer to leave it off so Vim's registers and the system clipboard stay separate; "+ is explicit and safe.

Read-only registers

You can read special registers to avoid typing: ". holds the last inserted text, ": the last command (run it again with @:), "% the current file name (Ctrl-r % in a command line fills it in), "/ the last search. For example :e <Ctrl-r>% or "%p pastes the file name into the text.

The expression register "= evaluates an expression: in Insert mode type Ctrl-r = then 6*7<Enter> and 42 is inserted. It is a built-in calculator.

JS Expression register as a calculator (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Editing a register

Registers are text, so you can edit them. Paste a macro or a register into a buffer with "ap, change it, then yank it back with "ayy. This is the standard way to fix a macro (see macros).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which register always holds the last yank?

    Yanks go to the unnamed register and to register 0; deletes only overwrite the unnamed one.
  2. 2. How do you delete text without changing any register?

    The black hole register "_ discards text.
  3. 3. How do you append a yanked line to register a?

    An uppercase register name appends to the lowercase one.
  4. 4. How do you paste from the system clipboard?

    The + register is the system clipboard (requires clipboard support).

Summary

  • "{reg} before a command picks a register; no register means the unnamed one.
  • Yanks fill "0; big deletes fill "1 to "9; small deletes fill "-.
  • Named registers a-z store your snippets, and A-Z append.
  • "_ is the black hole; "+ is the system clipboard.
  • "., ":, "/, "% and "= are read-only helpers.

Next: Macros: Record and Replay Edits