如何折叠区域中的空白?

发布于 2024-12-23 18:40:28 字数 667 浏览 2 评论 0原文

假设我有这个列表文本文件:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

但可醉心 2024-12-30 18:40:28

这个函数应该可以解决问题:

(defun just-one-space-in-region (beg end)
  "replace all whitespace in the region with single spaces"
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (re-search-forward "\\s-+" nil t)
        (replace-match " ")))))

并且,由于问题已更新以适用于矩形中的空间,请尝试以下操作:

(require 'rect)  
(defun just-one-space-in-rect-line (start end)
  (save-restriction
    (save-match-data
      (narrow-to-region (+ (point) start)
                        (+ (point) end))
      (while (re-search-forward "\\s-+" nil t)
        (replace-match " ")))))
(defun just-one-space-in-rect (start end)
  "replace all whitespace in the rectangle with single spaces"
  (interactive "r")
  (apply-on-rectangle 'just-one-space-in-rect-line start end))

This function should do the trick:

(defun just-one-space-in-region (beg end)
  "replace all whitespace in the region with single spaces"
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region beg end)
      (goto-char (point-min))
      (while (re-search-forward "\\s-+" nil t)
        (replace-match " ")))))

And, since the question was updated to apply to spaces in a rectangle, try this:

(require 'rect)  
(defun just-one-space-in-rect-line (start end)
  (save-restriction
    (save-match-data
      (narrow-to-region (+ (point) start)
                        (+ (point) end))
      (while (re-search-forward "\\s-+" nil t)
        (replace-match " ")))))
(defun just-one-space-in-rect (start end)
  "replace all whitespace in the rectangle with single spaces"
  (interactive "r")
  (apply-on-rectangle 'just-one-space-in-rect-line start end))
旧话新听 2024-12-30 18:40:28

没有真正回答您的问题,但当

 M-SPC runs the command just-one-space, which is an interactive
 compiled Lisp function in `simple.el'.

 It is bound to M-SPC.

 (just-one-space &optional N)

 Delete all spaces and tabs around point, leaving one space (or N spaces).

 [back]

您想一次性删除空格时,这很有用。它可能适用于删除是随机行且没有固定模式的宏观情况。

Not really answering your question but there is

 M-SPC runs the command just-one-space, which is an interactive
 compiled Lisp function in `simple.el'.

 It is bound to M-SPC.

 (just-one-space &optional N)

 Delete all spaces and tabs around point, leaving one space (or N spaces).

 [back]

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.

白芷 2024-12-30 18:40:28

您可以使用 cua-mode 的矩形编辑工具*来处理您的矩形要求。

  1. Mx cua-selection-mode RET(cua-selection-mode 1)
    (我自己,我永久启用了此功能)。

  2. 使用 C-RET 和普通移动键标记要折叠空白的矩形。

  3. 调用cua-replace-in-rectangle
    先生 \s-+ RET SPC RET

  4. C再次 -RET 结束矩形编辑。

(*) 如果您需要矩形右侧的字段保持对齐,则此方法不起作用,因为在这种情况下,您需要插入新的空格来补偿已删除的空格。您可以使用 2C-split 和 2C-merge 或直接删除/拉出最右边的矩形来解决这个问题。

You can deal with your rectangle requirements using cua-mode's rectangle editing facilities*.

  1. M-x cua-selection-mode RET or (cua-selection-mode 1)
    (myself, I have this enabled permanently).

  2. Mark the rectangle within which you wish to collapse the whitespace, using C-RET and the normal movement keys.

  3. Call cua-replace-in-rectangle:
    M-r \s-+ RET SPC RET

  4. C-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 and 2C-merge or just kill/yank the right-most rectangle to work around that.

会傲 2024-12-30 18:40:28

您使用 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.)

油焖大侠 2024-12-30 18:40:28

如果您不想使用cua-selection-mode,您可以使用矩形标记模式或鼠标选择一个矩形,然后进行空白替换。

简短版本:

Cx SPC 选择区域,然后 CM-% \s-+ Enter SPC Enter !

将用其中的单个空格替换所有连续的空白实例指定的矩形


长版:

  1. 选择要编辑的区域

    • 您可以按住CM,然后使用mouse-1单击并拖动以选择矩形。 (不适用于我的 emacs 版本,但在 中提到官方文档。)

    • 或者将点放在您想要矩形开始的位置,点击CxSPC进入矩形标记模式并然后移动光标,使您想要的区域突出显示

  2. 使用replace-regexp删除多余的空格

    • 点击CM-%激活replace-regexp,它现在应该提示您输入要搜索的模式
    • 输入 \s-+ 作为搜索模式,然后按 Enter(匹配一个或多个空格字符)
    • 输入您想要替换空格的内容,在这种情况下,您可以通过点击 SPC 然后 Enter 使用空格
  3. 执行替换

    • 现在所有要替换的空格都应该突出显示
    • 点击一次全部替换
    • 或者您可以通过点击 ySPC 逐一替换它们

If you don't want to use cua-selection-mode you can use rectangle-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:

  1. 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 highlighted

  2. Remove excess whitespace using replace-regexp

    • Hit C-M-% to activate replace-regexp, it should now prompt you for the pattern you want to search for
    • Enter \s-+ as the search pattern, then hit Enter (matches one or more white space characters)
    • Enter what you want to replace the white space with, in this case you can just use a space by hitting SPC and then Enter
  3. Perform the replacement

    • Now all of the whitespace to be replaced should be highlighted
    • Hit ! to replace them all at once
    • Or you can replace them one-by-one by hitting y or SPC for each one
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文