如何折叠区域中的空白?
假设我有这个列表文本文件:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
如何将其转换为:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
我知道我可以在该区域上使用 replace-regexp
,但 Emacs 正则表达式并不是自然而然的。我一直在寻找类似 delete-whitespace-rectangle
的东西,但这并没有达到我的预期,或者我误用了它。能够为每列执行此操作也是可取的,即:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
Let's say I have this tabulated text file:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
How can I transform it to:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
I know I could use replace-regexp
on the region, but Emacs regexps don't come naturally. I was looking for something like delete-whitespace-rectangle
, but that does not do what I expect, or I am misusing it. Having the ability to do this per-column would be desirable too, i.e:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这个函数应该可以解决问题:
并且,由于问题已更新以适用于矩形中的空间,请尝试以下操作:
This function should do the trick:
And, since the question was updated to apply to spaces in a rectangle, try this:
没有真正回答您的问题,但当
您想一次性删除空格时,这很有用。它可能适用于删除是随机行且没有固定模式的宏观情况。
Not really answering your question but there is
which is useful when you want to delete whitespace in one off cases. It might be suitable for a macro case where the deletions are at random lines with no fixed pattern.
您可以使用 cua-mode 的矩形编辑工具*来处理您的矩形要求。
Mx
cua-selection-mode
RET 或(cua-selection-mode 1)
(我自己,我永久启用了此功能)。
使用 C-RET 和普通移动键标记要折叠空白的矩形。
调用
cua-replace-in-rectangle
:先生
\s-+
RET SPC RETC再次 -RET 结束矩形编辑。
(*) 如果您需要矩形右侧的字段保持对齐,则此方法不起作用,因为在这种情况下,您需要插入新的空格来补偿已删除的空格。您可以使用 2C-split 和 2C-merge 或直接删除/拉出最右边的矩形来解决这个问题。
You can deal with your rectangle requirements using cua-mode's rectangle editing facilities*.
M-x
cua-selection-mode
RET or(cua-selection-mode 1)
(myself, I have this enabled permanently).
Mark the rectangle within which you wish to collapse the whitespace, using C-RET and the normal movement keys.
Call
cua-replace-in-rectangle
:M-r
\s-+
RET SPC RETC-RET again to end rectangle editing.
(*) This won't work if you need fields to the right of the rectangle to remain aligned, as in that case you would need to insert new spaces to compensate for the ones which were removed. You could use
2C-split
and2C-merge
or just kill/yank the right-most rectangle to work around that.您使用 IDE 吗?如果您使用的是 Eclipse 之类的工具,那么您可以格式化其中的空白选项。 (在 Eclipse 中,按 CTRL+3 并搜索“formatter”。很抱歉没有记住确切的路径!)然后,您应该能够突出显示所有内容并按 ctrl+shift+G 自动=格式化所有内容。
如果有帮助请告诉我们! (或者,如果您想启动执行此操作的代码,请告诉我们该语言。)
Are you using an IDE? If you're using something like Eclipse, then you could format whitespace options within it. (In Eclipse, press CTRL+3 and search for 'formatter'. I apologize for not remembering the exact path!) Then, you should be able to highlight everything and press ctrl+shift+G to auto=format everything.
Let us know if it helps! (Or, if you'd like to spinup code that will do this, let us know the language.)
如果您不想使用cua-selection-mode,您可以使用矩形标记模式或鼠标选择一个矩形,然后进行空白替换。
简短版本:
Cx SPC 选择区域,然后 CM-%
\s-+
Enter SPC Enter !将用其中的单个空格替换所有连续的空白实例指定的矩形
长版:
选择要编辑的区域
您可以按住CM,然后使用mouse-1单击并拖动以选择矩形。 (不适用于我的 emacs 版本,但在 中提到官方文档。)
或者将点放在您想要矩形开始的位置,点击CxSPC进入
矩形标记模式
并然后移动光标,使您想要的区域突出显示使用
replace-regexp
删除多余的空格replace-regexp
,它现在应该提示您输入要搜索的模式\s-+
作为搜索模式,然后按 Enter(匹配一个或多个空格字符)执行替换
If you don't want to use
cua-selection-mode
you can userectangle-mark-mode
or the mouse to select a rectangle and then do the white space replacement.Short version:
C-x SPC to select the region, then C-M-%
\s-+
Enter SPC Enter !Will replace all contiguous instances of whitespace with a single space within the specified rectangle
Long Version:
Select the region you want to edit
You can hold down C-M and then click and drag with mouse-1 to select a rectangle. (Doesn't work on my emacs version but is mentioned in the official docs.)
Or put the point where you want the rectangle to start, hit C-x SPC to enter
rectangle-mark-mode
and then move the cursor so that the region you want is highlightedRemove excess whitespace using
replace-regexp
replace-regexp
, it should now prompt you for the pattern you want to search for\s-+
as the search pattern, then hit Enter (matches one or more white space characters)Perform the replacement