我在.
vimrc中有这个删除尾部空格:
function! RemoveTrailingWhitespace() for lineno in range(a:firstline,a:lastline) let line = getline(lineno) let cleanLine = substitute(line,'\(\s\|\)\+$','','e') call setline(lineno,cleanLine) endfor endfunction command -range RemoveTrailingWhitespace <line1>,<line2>call RemoveTrailingWhitespace() command -range RT <line1>,<line2>call RemoveTrailingWhitespace()
这允许我调用:’<,'> RT来删除视觉上选定的行范围的尾随空格.当我只调用:RT时,它只在当前行上运行.我想要的是将命令应用于整个缓冲区.怎么能实现这一目标?
如果不指定范围,则带有范围的命令将应用于当前行.如果要在整个缓冲区上执行此操作,请使用:%RT或:1,$RT
原文链接:https://www.f2er.com/bash/384202.html将整个缓冲区作为默认范围可以做的是:
command -range=% RT <line1>,<line2>call RemoveTrailingWhitespace()
详情:
:h command-range
然后你看到:
Possible attributes are: -range Range allowed,default is current line -range=% Range allowed,default is whole file (1,$) -range=N A count (default N) which is specified in the line number position (like |:split|); allows for zero line number. -count=N A count (default N) which is specified either in the line number position,or as an initial argument (like |:Next|). Specifying -count (without a default) acts like -count=0
如果你有范围信息,为什么不在命令中调用vim-build:[range]来进行替换?那么你可以保存那些线getline,setline,也就是循环.