gpt4 book ai didi

vim - 将命令范围传递给函数

转载 作者:行者123 更新时间:2023-12-03 20:18:48 25 4
gpt4 key购买 nike

我正在尝试定义一个可以接受范围并将其传递给函数的命令。这是我认为应该拥有的:

function! PrintGivenRange() range
echo "firstline ".a:firstline." lastline ".a:lastline
" Do some more things
endfunction

command! -range PassRange call PrintGivenRange()


但是,它不是那样工作的,它似乎只能通过第一行。

例如

:1,5PassRange "outputs firstline 1 lastline 1
:1,5call PrintGivenRange() "outputs firstline 1 lastline 5
" if you select lines in visual mode, same thing for both


我已经读过 :help command-range,但仍无法弄清楚。我应该将前缀中的范围传递给 call吗?我该如何解决?

最佳答案

您需要显式传递范围,请尝试:

command! -range PassRange <line1>,<line2>call PrintGivenRange()

关于vim - 将命令范围传递给函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10572996/

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