How do I delete text and save it to a specific named register?
Answer
"add
Explanation
How it works
When you delete text in Vim with commands like dd, dw, or x, the deleted text goes into the unnamed register and the numbered registers (1-9). But if you want to save the deleted text in a specific named register for later use, you can prefix the delete command with " followed by the register letter.
The command "add breaks down as:
"a-- select registeraas the targetdd-- delete the entire current line
The deleted line is now stored in register a and can be pasted later with "ap. This works with any delete command:
"adw-- delete a word into registera"ad$-- delete to end of line into registera"ad}-- delete to end of paragraph into registera"adiw-- delete inner word into registera
The text is both deleted from the buffer and stored in the register, essentially giving you a cut operation that targets a specific register.
Example
Suppose you are reorganizing a file and want to move several non-adjacent lines to a new section:
- Go to the first line to move:
"add(cuts line into registera) - Go to the second line:
"Add(appends to registerausing uppercase) - Go to the third line:
"Add(appends again) - Navigate to where you want these lines:
"ap(pastes all three lines)
Note the uppercase A in step 2 and 3. Using the uppercase version of a register letter appends to the register instead of overwriting it. This is a powerful technique for collecting scattered lines.