gpt4 book ai didi

VIM:命令/功能的可选行范围

转载 作者:行者123 更新时间:2023-12-04 10:19:05 24 4
gpt4 key购买 nike

我的.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时,它仅在当前行上运行。我想要的是将命令应用于整个缓冲区。如何做到这一点?

最佳答案

如果不提供range,则带有range的命令将应用于当前行。如果要在整个缓冲区上执行此操作,请使用:%RT:1,$RT
您可以做的将整个缓冲区设置为默认范围的操作是:

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

对您的功能有一个评论/问题

如果您有范围信息,为什么不直接在 :[range]s命令中调用vim-build进行替换呢?那么您可以保存这些行 getlinesetline以及 loop

关于VIM:命令/功能的可选行范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16164518/

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