# Vim Registers Not Saving

You yank text with y, try to paste with p, and get old content instead of what you just copied. Or registers seem to empty themselves randomly. Understanding Vim's register system is key to fixing these issues.

Understanding Registers

Vim has multiple registers for different purposes:

RegisterNamePurpose
"UnnamedLast yank or delete
0YankLast yanked text
1-9NumberedLast 9 deletes
a-zNamedUser-named registers
A-ZNamed (append)Append to named register
_Black holeDiscard text
+System clipboardSystem clipboard
*Primary selectionX11 primary selection
/SearchLast search pattern
:CommandLast command
-Small deleteDelete less than one line

Check Register Contents

View all registers:

vim
:registers
" Or shortened
:reg

View specific registers:

vim
:reg 0 a b +

This shows registers 0, a, b, and the clipboard.

Yank Not Updating Unnamed Register

The unnamed register (") is overwritten by both yank and delete operations. If you yank, then delete, your yank is lost.

Solution: Use the yank register (0) which only holds yanked text:

```vim " Yank text yaw

" Delete something else (overwrites unnamed register) dw

" Paste the original yank "0p ```

Using Named Registers

For persistent storage, use named registers:

```vim " Yank to register a "ayy

" Paste from register a "ap ```

Named registers persist until you overwrite them.

Append to Named Registers

Use uppercase to append:

```vim " Yank line to register a "ayy

" Append another line to register a "Ayy

" Register a now contains both lines "ap ```

Black Hole Register

Prevent overwriting registers:

```vim " Delete without affecting registers "_dd

" Yank won't replace this text in any register ```

Clipboard Issues

System Clipboard Not Working

Check if Vim was compiled with clipboard support:

vim
:echo has('clipboard')

If 0, install a Vim version with clipboard:

```bash # Ubuntu/Debian sudo apt install vim-gtk3

# Fedora sudo dnf install vim-enhanced

# macOS (should work out of the box) # Try re-linking brew unlink vim && brew link vim ```

Copy to System Clipboard

```vim " Yank to system clipboard "+yy

" Paste from system clipboard "+p ```

Make Unnamed Register Use Clipboard

vim
set clipboard=unnamed

Now y and p automatically use the system clipboard.

For both primary and clipboard:

vim
set clipboard=unnamedplus,unnamed

X11 Primary Selection (Linux)

On Linux with X11, the * register is the primary selection (middle-click paste):

```vim " Yank to primary selection "*yy

" Paste from primary selection "*p ```

Registers Cleared Between Sessions

Registers don't persist by default. To save them:

Enable Viminfo

vim
set viminfo='100,<50,s10,h,!
  • '100 - Remember marks for 100 files
  • <50 - Save up to 50 lines per register
  • s10 - Skip registers larger than 10KB
  • h - Don't save hlsearch
  • ! - Save global variables

Save Session

Save the entire session including registers:

vim
:mksession ~/.vim/session.vim

Restore:

vim
:source ~/.vim/session.vim

Or start Vim with:

bash
vim -S ~/.vim/session.vim

Macros and Registers

Macros use registers. Recording a macro overwrites that register:

vim
qa    " Start recording to register a
...
q     " Stop recording

Now register a contains the macro. Use it:

vim
@a    " Execute macro from register a

Preserve Register While Recording Macro

If you need register a for both text and macro:

```vim " Store text in register b "bY

" Record macro to register a qa ... q

" Text is still in register b "bp ```

Search Register Issues

The / register holds the last search pattern:

```vim " Paste last search pattern "/p

" Set search pattern from register let @/ = "pattern" ```

Command Register

The : register holds the last command:

vim
" Paste last command
":p

Expression Register

Insert calculated values:

```vim " In insert mode, press: Ctrl-R =

" Then type an expression 2+2 " Result: 4 is inserted ```

Register Not Updating

If registers seem stuck:

Check viminfo Permissions

bash
ls -la ~/.viminfo

You should own this file. Fix permissions:

bash
chmod 600 ~/.viminfo

Clear viminfo

If corrupted:

bash
rm ~/.viminfo

Vim will recreate it.

Neovim Specifics

Neovim uses shada instead of viminfo:

vim
set shada='100,<50,s10,h

Check shada file:

bash
ls ~/.local/share/nvim/shada/

Plugin Conflicts

Some plugins manipulate registers. Check with:

vim
:verbose set clipboard?

And for register mappings:

vim
:map "

Troubleshooting Checklist

  1. 1.Check register contents: :reg
  2. 2.Verify clipboard support: :echo has('clipboard')
  3. 3.Check clipboard setting: :set clipboard?
  4. 4.Verify viminfo is writable
  5. 5.Use "0 for last yank
  6. 6.Use named registers for persistent text
  7. 7.Check for plugin conflicts

Quick Reference

```vim " View registers :reg

" Use specific register "ay " Yank to register a "ap " Paste from register a

" System clipboard "+y " Yank to clipboard "+p " Paste from clipboard

" Primary selection (X11) "*y " Yank to primary "*p " Paste from primary

" Auto-use clipboard set clipboard=unnamed

" Black hole (don't save) "_d " Delete without saving

" Yank register (always has last yank) "0p " Paste last yanked text

" Append to register "Ay " Append yank to register a ```

Understanding registers gives you powerful text manipulation capabilities. Use them deliberately and they'll work reliably.