Overview

A register is Vim’s internal clipboard.

Unlike a normal editor that has one clipboard, Vim has many registers that can store:

  • Yanked (copied) text
  • Deleted text
  • Recorded macros
  • Search patterns
  • Commands
  • File names
  • Expressions

Think of them as named storage locations.


View Registers

Show all non-empty registers:

:reg

or

:registers

Show specific registers:

:reg a
:reg a b c

Empty registers are not displayed.


Unnamed Register (")

The default register.

Commands like:

yy
dd
x

store text into the unnamed register.

Paste normally:

p
P

uses this register.


Named Registers (a-z)

Store text into a register:

"ayy

Paste:

"ap

Example:

Line 1
Line 2

Copy line 1:

"ayy

Move elsewhere:

"ap

Append to a Register

Lowercase replaces:

"ayy

Uppercase appends:

"Ayy

Useful for collecting multiple snippets.


Numbered Registers

RegisterPurpose
0Last yank
1Last delete
2-9Older deletes

Example:

yy
dd

Now:

p

pastes deleted text.

While

"0p

still pastes the last yanked text.


Black Hole Register (_)

Delete without changing any register.

"_dd

Useful when you want to preserve your previous yank.


System Clipboard

If a clipboard provider exists:

Copy:

"+y

Paste:

"+p

On remote SSH servers this usually does not work unless clipboard forwarding (e.g. OSC52) is configured.


Insert Register Contents

While in Insert mode:

<C-r>a

Insert contents of register a.

Insert last yank:

<C-r>0

Insert unnamed register:

<C-r>"

Very useful while typing.


Expression Register

Evaluate expressions.

In Insert mode:

<C-r>=2+3

Press Enter.

Result:

5

Macro Registers

Start recording into register a:

qa

Perform edits.

Stop:

q

Replay:

@a

Repeat last macro:

@@

Macros are simply registers containing keystrokes.


Useful Read-only Registers

RegisterMeaning
%Current file name
:Last command
/Last search
.Last inserted text
Example:
:echo @%

Or insert current filename while typing:

<C-r>%

Practical Commands

CommandDescription
:regShow registers
"ayyYank into register a
"apPaste from register a
"AyyAppend to register a
"0pPaste last yank
"_ddDelete without overwriting registers
"+yCopy to system clipboard
<C-r>aInsert register a
@aRun macro in register a

Recommended Daily Usage

These are the commands most worth memorizing:

:reg
"ayy
"ap
"0p
"_dd
<C-r>"

Once these become muscle memory, registers become one of Vim’s most powerful editing features.


Practice

Create a file with several lines.

  1. Copy line 1 into register a.
"ayy
  1. Copy line 2 into register b.
"byy
  1. Paste both.
"ap
"bp
  1. Delete a line without losing the copied text.
"_dd
  1. Paste the last yank.
"0p
  1. Inspect the registers.
:reg

You should now see registers a, b, 0, and the unnamed register populated.