在 Vim 中编辑时删除各种空格
很多时候,在使用 Vim 进行编辑时,我会遇到各种空白给我带来麻烦的情况。例如,假设我有这样的评论:
#This program was featured on the Today show, it is an algorithm for promoting world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause
例如,如果我想修剪这些行,以便它们转到 X 个字符,我最终会在顶行中间的某个位置放置一个换行符来得到这个(在点击换行和自动缩进):
#This program was featured on the Today show, it is an algorithm for promoting
world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause
然后我在行的开头添加一个#,这一切都很好,但我也希望该行对齐。为此,我必须删除换行符、下一行缩进的所有空格,然后删除注释 # 标记。这样做并不需要很长的时间,但这种情况和类似的情况加起来都需要一天的编码时间。
上面的例子非常具体,但我的问题不是。在 Vim 中删除所有空格(包括换行符)直到下一个非空格字符有什么好方法?如果 Vim 已经有可以做到这一点的动作,那就太棒了,但如果没有,是否有人有最喜欢的 Vim 函数,他们用它来执行上述操作,可以映射到一个键?至少,我是否错过了一些 Vim 使用习惯,让我不必担心这种情况?
编辑:格式化宽度虽然有用且适用于上述情况,但不是这个问题的重点。我更关心空白删除,它不会在行尾停止,而是继续到下一行的第一个非空白字符。
So oftentimes, while editing with Vim, I'll get into a variety of situations where whitespace gives me hassle. For example, say I have a comment like this:
#This program was featured on the Today show, it is an algorithm for promoting world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause
If I want to, for example, trim the lines so they go to X characters, I end up putting a newline somewhere in the middle of the top line to get this (after hitting the newline and auto-indenting):
#This program was featured on the Today show, it is an algorithm for promoting
world peace in third-world countries
#given the name of that country and the name of a celebrity to endorse its cause
I then add a # to the beginning of the line, and that's all well and good, but then I want that line to line up, too. To do so, I have to delete the newline, all the whitespace for the indent on the next line, and then the commenting # mark. It doesn't take an awfully long amount of time to do that, but this and similar situations all add up over a day's worth of coding.
Now the example above is pretty specific, but my question isn't. What's a good way in Vim to delete all whitespace INCLUDING NEWLINES up until the next non-whitespace character? If Vim already has movements that do that, that would be awesome, but if not, does anyone have a favorite Vim function they use to do the above that could be mapped to a key? At the very least, am I missing some Vim usage idiom that prevents me from even having to worry about this case?
EDIT: Formatting to width, while useful and applicable to the case above, isn't the focus of this question. I'm concerned more with whitespace removal that doesn't stop at the end of a line, but instead carries on to the first non-whitespace character of the next line.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您实际上只想重新格式化该注释以适合当前的
'textwidth'
。如果注释是一个段落(即上下用一行空格分隔),那么您可以使用gqip
(gq
是重新格式化命令,ip
是“内部段落”文本对象)以重新格式化它。如果它不是一个独立的段落,您可以直观地选择这些行,然后使用gq
。这可能还依赖于正确设置
'formatoptions'
以确保正确处理注释字符,但在许多情况下 ftplugin 已经做到了这一点。You really just want to reformat that comment to fit the current
'textwidth'
. If the comment is a paragraph (i.e., separated by a line of whitespace above and below), then you can just usegqip
(gq
is the reformat command,ip
is the "inner-paragraph" text object) to reformat it. If it's not a standalone paragraph, you can visually select those lines and then usegq
.This likely also relies on having
'formatoptions'
set correctly to make sure the comment characters are handled properly, but in many cases the ftplugin has already done that.这是一段时间之后的事情,但我发现有一个命令可以在 90% 的情况下完成我需要的操作:
This is a while later, but I found that there is a command that does what I need to in 90% of circumstances:
该命令似乎有效:
它使用替换来删除行尾之前的空格以及末尾的新行。
在此示例中:(
要在 vim 中查看元字符,请使用命令
:set list
)如果将光标放在第一行并使用第一个命令,它将删除从 aad 到 $ 的所有内容(不包括aad 但包括 $ 和换行符。)
另外,请注意您正在做的事情,使用外部程序为您格式化注释要高效得多。特别是, par 是一个很棒的小型 C 程序,可以编辑文本并将其包装到所需的长度。
如果您的路径中有
par
,那么执行您想要执行的操作就像使用Shift+v
选择注释块并运行命令(其中 40 是) 一样简单所需的列宽度。
如果您觉得很黑客,请用 C/perl/C++/python 编写自己的程序,随意编辑注释,然后将其放入路径中并使用外部过滤器命令
:!
来处理文本块通过它。This command seems to work:
it uses a replace to remove whitespace up to end of line and the new line at the end.
In this example:
(to see meta characters in vim use the command
:set list
)if you place the cursor on the first line and use the first command it will delete everything from aad to $ (not including aad but including $ and a newline.)
Also, note for what you are doing it is far more efficient to use an external program to format comments for you. In particular, par is a great small C program that edits text and wraps it to desired lengths.
If you have
par
in your path, to do what you are trying to do is as easy as selecting the block of comment withShift+v
and running the commandwhere 40 is the desired width in columns.
If you are feeling hackish, write your own program in C/perl/C++/python that edits comments however you like, then put it in path and use the external filter command
:!
to process blocks of text through it.