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
gris the replace-with-register operator{motion}is any motion or text object:w,iw,ap,$, etc.grrreplaces the entire current line"agriwuses registerato 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, pressgriwthen.to repeat grris the linewise variant — replaces the whole current line- Works with any text object:
gri(,grap,gr$, etc. - Use
"0griwto replace from the yank register, avoiding interference from delete operations