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

How do I replace a text object with the contents of a register in Neovim 0.11?

Answer

gr{motion}

Explanation

Neovim 0.11 introduced gr as a built-in operator that replaces any text object with the contents of the unnamed register (or a named register with "x). Previously, this required the ReplaceWithRegister plugin — now it works out of the box.

How it works

  • gr is the replace-with-register operator
  • {motion} is any motion or text object: w, iw, ap, $, etc.
  • grr replaces the entire current line
  • "agriw uses register a to replace the inner word
  • The replaced text is discarded — it does not overwrite the source register

Example

Yank a word with yiw, then position the cursor over a different word and run:

griw

Before:

foo = bar

With bar in the unnamed register and cursor on foo:

bar = bar

The original foo is discarded without touching the register, so you can keep replacing with ..

Tips

  • Combine with . to replace multiple occurrences one at a time: yank a word, move to each target, press griw then . to repeat
  • grr is the linewise variant — replaces the whole current line
  • Works with any text object: gri(, grap, gr$, etc.
  • Use "0griw to replace from the yank register, avoiding interference from delete operations

Next

What is the difference between the inner word (iw) and inner WORD (iW) text objects in Vim?