gpt4 book ai didi

vim - 如何用参数定义一个新的 Vim 运算符?

转载 作者:行者123 更新时间:2023-12-03 11:41:58 24 4
gpt4 key购买 nike

我一直在寻找在 Vim 中映射一个带有额外参数的新运算符。
例如,我们知道 ciw将“切入单词”并将您置于插入模式。我正在寻找的是有一个自定义操作来替换 c (例如, s )进行类似 iw 的 Action ,但需要一个额外的参数。
一个简单的例子是:

Given a line in a text file
执行 siw*在普通模式下(假设光标位于第一列),它用 * 包围第一个单词像这样:
*Given* a line in a text file
我知道,这是最优秀的 surround.vim插件可以。但我只是在这里举一个例子,并寻找关于如何获取映射以便上述工作的答案。
我试过玩 onoremapopfunc ,但似乎无法让他们按照我想要的方式演奏。
所以,我正在寻找的是运动加上运算符(operator)挂起映射的组合。

最佳答案

下面是一个示例实现中描述的命令
问题,用于说明目的。

nnoremap <silent> s :set opfunc=Surround<cr>g@
vnoremap <silent> s :<c-u>call Surround(visualmode(), 1)<cr>

function! Surround(vt, ...)
let s = InputChar()
if s =~ "\<esc>" || s =~ "\<c-c>"
return
endif
let [sl, sc] = getpos(a:0 ? "'<" : "'[")[1:2]
let [el, ec] = getpos(a:0 ? "'>" : "']")[1:2]
if a:vt == 'line' || a:vt == 'V'
call append(el, s)
call append(sl-1, s)
elseif a:vt == 'block' || a:vt == "\<c-v>"
exe sl.','.el 's/\%'.sc.'c\|\%'.ec.'c.\zs/\=s/g|norm!``'
else
exe el 's/\%'.ec.'c.\zs/\=s/|norm!``'
exe sl 's/\%'.sc.'c/\=s/|norm!``'
endif
endfunction
要获取用户输入,函数 InputChar()被使用,假设
所需的参数是单个字符。
function! InputChar()
let c = getchar()
return type(c) == type(0) ? nr2char(c) : c
endfunction
如果需要接受字符串参数,请将调用更改为 InputChar()Surround()调用 input() , 反而。

关于vim - 如何用参数定义一个新的 Vim 运算符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8994276/

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