Vimscript 函数范围只适用于第一行
Vimscript function range only applying to the first line
我正在尝试创建自己的评论功能,以供 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()
但是调用给定范围(:'<,'>Comment
)的命令时只有第一行的选择在前面加上#
,没有报其他错误。
要替换范围内的每一行,我缺少什么?
与映射不同(在可视模式下调用时会自动将 :'<,'>
添加到函数 :call
前面),自定义命令需要显式传递范围:
command! -range Comment <line1>,<line2>call Comment()
不幸的是,:help :command-range
只提到了相关的 <count>
,但您可以在 :help <line1>.
中进一步找到相关信息
我正在尝试创建自己的评论功能,以供 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()
但是调用给定范围(:'<,'>Comment
)的命令时只有第一行的选择在前面加上#
,没有报其他错误。
要替换范围内的每一行,我缺少什么?
与映射不同(在可视模式下调用时会自动将 :'<,'>
添加到函数 :call
前面),自定义命令需要显式传递范围:
command! -range Comment <line1>,<line2>call Comment()
不幸的是,:help :command-range
只提到了相关的 <count>
,但您可以在 :help <line1>.