gpt4 book ai didi

Vim - 将寄存器传递给运算符(operator)函数

转载 作者:行者123 更新时间:2023-12-02 04:39:12 25 4
gpt4 key购买 nike

我正在尝试实现 ChangePaste 运算符。它应该用来自寄存器的文本替换文本。

它可以很好地处理 Action ,所以我可以使用 cp<motion>并且文本将从默认寄存器中替换。

现在我希望能够将它与不同的寄存器一起使用。我正在寻找如何将选定的寄存器传递给运算符(operator)函数的信息。所以,如果一种类型 "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 和缓冲区变量的解决方案:
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 - 将寄存器传递给运算符(operator)函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38882346/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com