Vimscript函数范围仅适用于第一行

jvi*_*tti 2 vim

我正在尝试创建自己的评论功能,用于vimscript学习目的.

我做了以下事情:

function! Comment() range
  for line_number in range(a:firstline, a:lastline)
    let current_line = getline(line_number)
    let current_line_commented = substitute(current_line, '^', '# ', "")
    call setline(line_number, current_line_commented)
  endfor
endfunction

command! -range Comment call Comment()
Run Code Online (Sandbox Code Playgroud)

但是,当使用给定范围(:'<,'>Comment)调用命令时,只有选择的第一行被#添加到前面,并且不会报告其他错误.

为了让范围内的每一行都被替换,我错过了什么?

Ing*_*kat 6

与映射(在可视模式下调用时自动获得:'<,'>函数的前置:call)不同,自定义命令需要显式传递范围:

command! -range Comment <line1>,<line2>call Comment()
Run Code Online (Sandbox Code Playgroud)

:help :command-range不幸的是,只提到了相关内容<count>,但你会发现更进一步的信息:help <line1>.