How do I copy the contents of one register into another register?
Answer
:let @a = @"
Explanation
Vim's :let command lets you read and write register contents as strings, making it possible to copy, combine, or modify register values without ever leaving the editor. Copying the unnamed register @" into a named register before it gets overwritten is a practical pattern for preserving yanked text during complex multi-step edits.
How it works
:letassigns an expression to a variable or register@aon the left side refers to the named registeraas an lvalue@"on the right side reads the current contents of the unnamed register- This copies the unnamed register's text into register
a, leaving both intact
You can copy between any two registers:
:let @b = @a " copy register a into b
:let @" = @a " copy named register a back into the unnamed register
:let @+ = @" " push unnamed register to the system clipboard
Example
You yank a line with yy, but then need to delete several lines (which would overwrite @"):
Before: @" contains "important text\n"
:let @a = @"
After: @a contains "important text\n", safe from future deletes
Now paste with "ap at any point, even after many deletions.
Tips
- Extend this to transform register contents:
:let @a = substitute(@a, 'foo', 'bar', 'g')runs a substitution on the register value itself :let @a = toupper(@a)uppercases everything in the register without a buffer roundtrip:reg ainspects the current value of registerabefore and after