Vimscript功能范围仅适用于第一行

Vimscript功能范围仅适用于第一行,vim,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(

为了学习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()

但是,在使用给定范围(
:“调用命令时,与映射不同(在可视化模式下调用时,会自动获取
:“我已经尝试了您的代码,但它确实给出了错误,因为
linenum
不存在——您可能在for的第一行重命名了它,而忘了更新其他引用。@mMontu将函数复制到堆栈溢出时,这可能是一个错误。现在修复,可以了。”在我的vimrc上。@Ammontu:由于你对语法错误的评论做了充分的准备,我能够轻松地通过……该死……我希望我能早点找到这篇文章——这会为我节省大量的时间!直到我开始问这个问题时,它才出现了。太好了!
command! -range Comment <line1>,<line2>call Comment()