map命令定义的函数,如何让命令不要执行v:count次

59 views
Skip to first unread message

火冷

unread,
Sep 3, 2019, 9:57:46 PM9/3/19
to Vim.cn
如题,有时候v:count只想当个参数使用,命令只执行一次,需要怎么做呢?谢谢!!

依云

unread,
Sep 4, 2019, 5:03:25 AM9/4/19
to Vim.cn
On Tue, Sep 03, 2019 at 06:57:46PM -0700, 火冷 wrote:
> 如题,有时候v:count只想当个参数使用,命令只执行一次,需要怎么做呢?谢谢!!

能说具体一点吗?map 的语义就是映射而已,它和 v:count 是正交的。map 命令
也无法定义函数。

自定义命令的话,定义的时候可以指定如何处理 count / range。(见 :help
:command-range)。

自定义函数也是可以自己处理的(见 :help func-range)。

--
Best regards,
lilydjwg

Linux Vim Python 我的博客:
https://blog.lilydjwg.me/
--
A: Because it obfuscates the reading.
Q: Why is top posting so bad?

火冷

unread,
Sep 9, 2019, 10:52:16 PM9/9/19
to Vim.cn
我是想处理当前光标字符,并和v:count1有关。 
比如当前光标下是 "零一二三四五六七八九十" 其中一个字符(假设“五”),我按2g=,结果变成“七”,按2g-,结果变成“三”。

之前map的写法如下,先s,再函数处理@"的内容,v:count1会影响s的操作
nnoremap g= s<C-r>=funcA(@",v:count1)<cr>

 
后来改成下面这样,当前光标字符在函数里获取,v:count1好像就不会影响了。
nnoremap g= :<C-u>call funcA(v:count1)<cr>

不过仍然不知道v:count1对于map是怎样的机制。
"当前光标字符+n
function! funcA(n) abort
    let l = line('.')
    let c = col('.')
    let s = getline(l)
    let char = strcharpart(strpart(s,c-1), 0, 1)
    let sBefore = strpart(s, 0, c-1)
    let sAfter = strcharpart(strpart(s, c-1), 1)
    let lStr = ['零一二三四五六七八九十', '零壹贰叁肆伍陆柒捌玖拾']
    let cNew = ''
    for k in lStr
        if stridx(k, char) != -1
            try | let cNew = strpart(k, stridx(k, char)+a:n*3, 3) | endtry
            break
        endif
    endfor
    "失败则字符asc码+n
    if !strlen(cNew) | let cNew = nr2char(char2nr(char)+a:n) | endif
    call setline(l, sBefore . cNew . sAfter)
endfunction



Reply all
Reply to author
Forward
0 new messages