I'm trying to write a "Paste-over" operator. It will work like a combination of p and R, so it will paste the contents of a register over the text currently in the buffer. I have this so far:

function! PasteOver(before)
  if a:before
    call feedkeys("m`")

  call feedkeys("R")
  for i in split(@", '\zs')
    call feedkeys(i)
  call feedkeys("\<esc>")

  if a:before
    call feedkeys("\<C-o>")

nnoremap <M-p> :<C-u>call PasteOver(0)<cr>
nnoremap <M-P> :<C-u>call PasteOver(1)<cr

And it works great. However, I have a dilemma. I'd like this operator to work with registers, the same way that p does, e.g. "a<M-p> will paste over from the contents of register 'a', and have this work with every register.

There's two ways I could think of to do this:

  • Write a mapping for every single register. I think it goes without saying that this is a terribly inefficient and sloppy way of doing this.

  • Make the register argument come after the operator, so "a<M-p> would become <M-p>a. This would work fine, but it makes pasting from the default register less convenient, and I prefer the first way since it feels more natural (since my fingers have already learned "ap

Is there a builtin way to get the register argument to an operator? I'm thinking something like v:count, but v:register instead.

  • 3
    Well, :help v:register and :help getreg().
    – romainl
    Jul 24, 2016 at 20:57

1 Answer 1


You can use v:register to grab the name of the last register supplied to a command, getreg() to grab its contents, and <c-r>= to insert text from a register when in insert (or replace) mode:

nnoremap <m-p> R<c-r>=getreg(v:register)<cr><esc>
nnoremap <m-P> R<c-r>=getreg(v:register)<cr><esc>`[

You can also use `[ to jump to the beginning of the last changed text.

  • Wow. This solution is way simpler than mine. And it effortlessly works with counts! +1
    – DJMcMayhem
    Aug 3, 2016 at 3:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.