vimtricks.wiki Concise Vim tricks, one at a time.

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 register a as the target
  • dd -- 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 register a
  • "ad$ -- delete to end of line into register a
  • "ad} -- delete to end of paragraph into register a
  • "adiw -- delete inner word into register a

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:

  1. Go to the first line to move: "add (cuts line into register a)
  2. Go to the second line: "Add (appends to register a using uppercase)
  3. Go to the third line: "Add (appends again)
  4. 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.

Next

How do you yank a single word into a named register?