gpt4 book ai didi

vim - 丢弃 映射中的计数

转载 作者:行者123 更新时间:2023-12-03 03:45:57 26 4
gpt4 key购买 nike

例如,我有以下表达式映射:

    function! DelNthSpace()        let col=match(getline('.'), '[[:blank:] ]', col('.'), v:count1)        return virtcol([line('.'), col+1])."|s \e"    endfunction    nnoremap  s DelNthSpace()
You see, v:count1 is already handled here. But if I try to replace second space with such function that is located, for example, at virtual column 35 (it will be if you place cursor on the return statement), then this tries to go to column 235 and replace it because this will be transformed from 2s to 2{DelNthSpace()} -> 235|s ^[. Now I am prepending bare useless | at the start of return statement which consumes unneeded count, but I do not like this solution as it uses two movements instead of one.

UPDATE: I should have provided more context:

function! ToNthSpace()
let col=match(getline('.'), '[[:blank:] ]', col('.'), v:count1)
return virtcol([line('.'), col+1])."|"
endfunction
noremap <expr> s ToNthSpace()

有很多函数正在使用这些函数,其中一个函数模仿替换命令,但音译其参数(因此,在调用 r$ii 时将用 $ 替换字符并插入 i\tr$ii 将替换字符与 t\tt 以及其他一些之间的关系相同),其他的是动议。由于这些函数定义了运动,我无法使用:<C-u> ,它将离开视觉模式(或者我将不得不添加一个 hack 来告诉从哪个模式函数被调用)。我不知道这个技巧,也无法为正常模式和视觉模式定义不同的映射。

最佳答案

没有办法让 vim 忽略 <expr> 的计数映射,但是您可以轻松更改映射以使您的函数正常工作:

nnoremap s :<C-U>exe 'normal!' DelNthSpace()<CR>

参见:help v:count有关使用 CTRL+U 的信息删除计数。

关于vim - 丢弃 <expr> 映射中的计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4261177/

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