I'm attemping to call a function when pressing y + motion like this in my vimrc:
function! s:MyFunction(reg)
echo a:reg
endfunction
nnoremap y :<C-U> call <SID>MyFunction(v:register)<CR>y
When I type "ayw, MyFunction echoes 'a'
like expected, but the yanked word is never written to register "a
, instead it is written to the yank register "0
.
Why and how to fix this so my mapping honors the specified named register?
y
is executed, the value ofv:register
is actually lost. That's why you see the value echo'ed, but after that command is finished, Vim resets v:register, so they
does not see the specified register anymore – Christian Brabandt Aug 1 '16 at 12:53