我正在尝试实现ChangePaste运算符。它应将文本替换为from寄存器中的文本。

它可以很好地配合运动,所以我可以使用cp<motion>,文本将从默认寄存器中替换。

现在,我希望能够与其他寄存器一起使用。我正在寻找有关如何将选定的寄存器传递给操作员功能的信息。因此,如果是"acpiw类型,我希望脚本用寄存器a内容替换一个内部单词。那有可能吗?

到目前为止的代码:

nmap <silent> cp :set opfunc=ChangePaste<CR>g@
function! ChangePaste(type, ...)
    if a:0  " Invoked from Visual mode, use '< and '> marks.
        silent exe "normal! `<" . a:type . "`>\"_c" . @"
    elseif a:type == 'line'
        silent exe "normal! '[V']\"_c" . @"
    elseif a:type == 'block'
        silent exe "normal! `[\<C-V>`]\"_c" . @"
    else
        silent exe "normal! `[v`]\"_c" . @"
    endif
endfunction

编辑:

使用v:register和buffer变量的解决方案:
nmap <silent> cp :let b:changepaste_buffer = v:register<cr>:set opfunc=ChangePaste<CR>g@
function! ChangePaste(type, ...)
    if a:0  " Invoked from Visual mode, use '< and '> marks.
        silent exe "normal! `<" . a:type . "`>\"_c" . getreg(b:changepaste_register)
    elseif a:type == 'line'
        silent exe "normal! '[V']\"_c" . getreg(b:changepaste_register)
    elseif a:type == 'block'
        silent exe "normal! `[\<C-V>`]\"_c" . getreg(b:changepaste_register)
    else
        silent exe "normal! `[v`]\"_c" . getreg(b:changepaste_register)
    endif
endfunction

最佳答案

正如@romainl在注释中提到的那样,您可以在函数中访问v:register变量。

甚至无需将其另存为缓冲区变量。

关于vim - Vim-将寄存器传递给运算符(operator)功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38882346/

10-13 07:15