# 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:
| Register | Name | Purpose |
|---|---|---|
" | Unnamed | Last yank or delete |
0 | Yank | Last yanked text |
1-9 | Numbered | Last 9 deletes |
a-z | Named | User-named registers |
A-Z | Named (append) | Append to named register |
_ | Black hole | Discard text |
+ | System clipboard | System clipboard |
* | Primary selection | X11 primary selection |
/ | Search | Last search pattern |
: | Command | Last command |
- | Small delete | Delete less than one line |
Check Register Contents
View all registers:
:registers
" Or shortened
:regView specific registers:
: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:
: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
set clipboard=unnamedNow y and p automatically use the system clipboard.
For both primary and clipboard:
set clipboard=unnamedplus,unnamedX11 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
set viminfo='100,<50,s10,h,!'100- Remember marks for 100 files<50- Save up to 50 lines per registers10- Skip registers larger than 10KBh- Don't savehlsearch!- Save global variables
Save Session
Save the entire session including registers:
:mksession ~/.vim/session.vimRestore:
:source ~/.vim/session.vimOr start Vim with:
vim -S ~/.vim/session.vimMacros and Registers
Macros use registers. Recording a macro overwrites that register:
qa " Start recording to register a
...
q " Stop recordingNow register a contains the macro. Use it:
@a " Execute macro from register aPreserve 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:
" Paste last command
":pExpression 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
ls -la ~/.viminfoYou should own this file. Fix permissions:
chmod 600 ~/.viminfoClear viminfo
If corrupted:
rm ~/.viminfoVim will recreate it.
Neovim Specifics
Neovim uses shada instead of viminfo:
set shada='100,<50,s10,hCheck shada file:
ls ~/.local/share/nvim/shada/Plugin Conflicts
Some plugins manipulate registers. Check with:
:verbose set clipboard?And for register mappings:
:map "Troubleshooting Checklist
- 1.Check register contents:
:reg - 2.Verify clipboard support:
:echo has('clipboard') - 3.Check clipboard setting:
:set clipboard? - 4.Verify viminfo is writable
- 5.Use
"0for last yank - 6.Use named registers for persistent text
- 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.