如何在 Vim 中修剪文件末尾的空行?
有时我不小心在正在编辑的文件末尾留下空行。
在 Vim 中保存时如何修剪它们?
更新
谢谢大家,所有解决方案似乎都有效。
不幸的是,它们都重置了当前光标位置,所以我编写了以下函数。
function TrimEndLines()
let save_cursor = getpos(".")
silent! %s#\($\n\s*\)\+\%$##
call setpos('.', save_cursor)
endfunction
autocmd BufWritePre *.py call TrimEndLines()
Sometimes I accidentally leave blank lines at the end of the file I am editing.
How can I trim them on saving in Vim?
Update
Thanks guys, all solutions seem to work.
Unfortunately, they all reset current cursor position, so I wrote the following function.
function TrimEndLines()
let save_cursor = getpos(".")
silent! %s#\($\n\s*\)\+\%$##
call setpos('.', save_cursor)
endfunction
autocmd BufWritePre *.py call TrimEndLines()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这个替代命令应该做到这一点:
请注意,这会删除所有仅包含空格的尾随行。要仅删除真正的“空”行,请从上述命令中删除
\s*
。编辑
解释:
\(
..... 开始匹配组$\n
... 匹配新行(结尾-行字符后跟回车符)\s*
...在此新行上允许任意数量的空格\)
..... 结束匹配组\%$
... 匹配文件末尾因此,正则表达式匹配任意数量的仅包含空格的相邻行,仅以文件末尾终止。然后,替换命令将匹配项替换为空字符串。
This substitute command should do it:
Note that this removes all trailing lines that contain only whitespace. To remove only truly "empty" lines, remove the
\s*
from the above command.EDIT
Explanation:
\(
..... Start a match group$\n
... Match a new line (end-of-line character followed by a carriage return).\s*
... Allow any amount of whitespace on this new line\)
..... End the match group\+
..... Allow any number of occurrences of this group (one or more).\%$
... Match the end of the fileThus the regex matches any number of adjacent lines containing only whitespace, terminated only by the end of the file. The substitute command then replaces the match with a null string.
1. 一个优雅的解决方案可以基于
:vglobal
命令(或者,在带有
!
修饰符的:global
上是同一件事):此命令在每行上执行
:delete
没有其中的非空白字符以及其后的其余字符
文本到缓冲区末尾(请参阅
:help /\s
、:help /\S
和:help /\_
来理解模式)。因此,该命令删除尾部
空白行。
严格意义上删除空行——而不是空白行
仅包含空格 - 更改
:vglobal
中的模式命令如下。
2. 在包含大块连续数据的巨大稀疏文件上
空白字符(从大约数百千字节开始)
空格)上述命令可能会产生不可接受的性能。
如果是这样的话,同样可以用优雅的想法来改造
:vglobal
命令进入速度更快(但可能更少优雅的)
:delete
命令,具有模式定义的范围。对于空行:
对于空行:
两个命令的本质是相同的;即删除行
属于指定范围,根据
分三步:
解释前将光标移动到缓冲区的第一行
范围的其余部分(
0;
- 请参阅:help :;
)。之间的区别0
和1
行号是前者允许在第一行,当有一个搜索模式用于定义结尾时
范围内的行。
搜索描述非尾随空白的模式所在的行
行(
\_s*\S
或\n*.
)不匹配(否定是由于\@!
atom — 请参阅:help /\@!
)。将范围的起始线设置为该行。
将范围的结束行设置为缓冲区的最后一行
(
,$
—参见:help :$
)。3. 要在保存时运行上述任何命令,请使用以下命令触发它
在
BufWrite
事件(或其同义词,BufWritePre
)。1. An elegant solution can be based on the
:vglobal
command(or, which is the same thing, on the
:global
with!
modifier):This command executes
:delete
on every line that does not havenon-whitespace characters in it, as well as after it in the remaining
text to the end of buffer (see
:help /\s
,:help /\S
, and:help /\_
to understand the pattern). Hence, the command removes the tailing
blank lines.
To delete the empty lines in a strict sense—as opposed to blank ones
containing only whitespace—change the pattern in that
:vglobal
command as follows.
2. On huge sparse files containing large blocks of consecutive
whitespace characters (starting from about hundreds of kilobytes of
whitespace) the above commands might have unacceptable performance.
If that is the case, the same elegant idea can be used to transform
that
:vglobal
commands into much faster (but perhaps lesselegantly-looking)
:delete
commands with pattern-defined ranges.For blank lines:
For empty lines:
The essence of both commands is the same; namely, removing the lines
belonging to the specified ranges, which are defined according to the
following three steps:
Move the cursor to the first line of the buffer before interpreting
the rest of the range (
0;
—see:help :;
). The difference between0
and1
line numbers is that the former allows a match at thefirst line, when there is a search pattern used to define the ending
line of the range.
Search for a line where the pattern describing a non-tailing blank
line (
\_s*\S
or\n*.
) does not match (negation is due to the\@!
atom—see:help /\@!
). Set the starting line of the range tothat line.
Set the ending line of the range to the last line of the buffer
(
,$
—see:help :$
).3. To run any of the above commands on saving, trigger it using
an autocommand to be fired on the
BufWrite
event (or its synonym,BufWritePre
).您可以将其放入 vimrc 中
(将
*.txt
替换为您想要的任何通配模式)详细信息:
BufWritePre
是将缓冲区写入文件之前的事件。$put _
在文件末尾附加一个空行(来自始终为空的寄存器)|
链 Ex 命令$;?\(^\s*$\ )\@!?
转到文件末尾 ($
),然后 (;
) 向后查找 (?...?
)对于不完全空白的第一行(\(^\s*$\)\@!
),另请参阅:help /\@!
了解 vim 搜索中的否定断言。×××+1,$
形成从第×××+1行到最后一行的范围d
删除该行范围。You can put this into your vimrc
(replace
*.txt
with whatever globbing pattern you want)Detail:
BufWritePre
is the event before writing a buffer to a file.$put _
appends a blank line at file end (from the always-empty register)|
chains Ex commands$;?\(^\s*$\)\@!?
goes to end of file ($
) then (;
) looks up backwards (?…?
) for the first line which is not entirely blank (\(^\s*$\)\@!
), also see:help /\@!
for negative assertions in vim searches.×××+1,$
forms a range from line ×××+1 till the last lined
deletes the line range.受到 @Prince Goulash 解决方案的启发,将以下内容添加到您的
~/.vimrc
中,以删除 Ruby 和 Python 文件每次保存时的尾随空白行:Inspired by solution from @Prince Goulash, add the following to your
~/.vimrc
to remove trailing blank lines for every save for Ruby and Python files:我发现之前使用替代的答案在操作非常大的文件时会引起麻烦并污染我的寄存器。这是我想出的一个函数,它对我来说性能更好,并且避免污染寄存器:
I found the previous answers using substitute caused trouble when operating on very large files and polluted my registers. Here's a function I came up with which performs better for me, and avoids polluting registers:
我有一个名为 Preserve 的独立函数,我可以从其他函数调用它,
它使使用 Preserve 执行其他操作变得很容易:
就我而言,我在文件末尾至少保留一个空行,但不超过一个。
这就是为什么我有一个单独的保留功能:
如果你想创建一个自动命令,你必须创建一个组
避免超载自动命令:
这是更改文件头(如果您有任何建议),请随意互动:
I have a separated function called Preserve which I can call from other functions,
It makes easy to use Preserve to do other stuff:
In my case, I keep at least one blank line, but not more than one, at the end of the file.
Here's why I have a separated Preserve function:
And if by any chance you want to create an autocommand you must create a group
to avoid overloading autocommands:
This one is to change file headers (if you have any suggestions) feel free to interact: