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

How do I replace the next word with a yanked word without clobbering the unnamed register?

Answer

yiww"_ciw\<C-r>0\<Esc>

Explanation

When you are doing repetitive refactors, cw is fast but it overwrites the unnamed register with the replaced text. This sequence avoids that by yanking once, moving to the next target, and changing through the black-hole register while pasting from register 0. It is a compact way to do repeatable replacements without losing your original yank.

How it works

  • yiw yanks the current word into register 0
  • w jumps to the next word
  • "_ciw changes that word using the black-hole register, so deleted text is discarded
  • \<C-r>0 inserts the contents of register 0 while in Insert mode
  • \<Esc> returns to Normal mode

This is especially useful when propagating an identifier or token across nearby positions while keeping register state stable for subsequent edits.

Example

Before:

alpha beta

Run with cursor on alpha:

yiww"_ciw<C-r>0<Esc>

After:

alpha alpha

Tips

  • Use b instead of w if your next target is backward.
  • Once the first replacement is done, . can often repeat the change pattern efficiently.

Next

How do I run a substitution across the arglist and write only modified files?