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`")
endif
call feedkeys("R")
for i in split(@", '\zs')
call feedkeys(i)
endfor
call feedkeys("\<esc>")
if a:before
call feedkeys("\<C-o>")
endif
endfunction
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.
:help v:register
and:help getreg()
.