2

I'm trying to write a custom yank function but am unable to figure out how to make it non-repeatable (like the normal yank). I have something similar to the following:

function! s:YankMotion(type)

    if a:type ==# 'line'
        normal! `[V`]y
    elseif a:type ==# 'char'
        normal! `[v`]y
    else
        echom "Unexpected selection type"
        return
    endif
endfunction

nnoremap y :set opfunc=<sid>YankMotion<cr>g@

The problem is that if you do an operation that is repeatable (eg. cw) then yank something, then execute repeat again hitting ., you would expect to trigger cw but instead it tries to do the yank again.

Is it possible to fix this?

1 Answers1

2

Vim will repeat the g@ command. Unfortunately, there's no way around this, and as you also cannot access the internal command history, it's impossible to restore the previously issued command. (You could hack something together for custom mappings that rely on repeat.vim, but it won't work for built-in commands.)

The only workaround I can think of is to change the mapping to use the normal y command, and trigger whatever is part of your "custom yank" afterwards, e.g. via :autocmd on CursorMoved,CursorHold.

Ingo Karkat
  • 23,523