如何折叠区域中的空白? [英] how to collapse whitespaces in a region?

查看:96
本文介绍了如何折叠区域中的空白?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

比方说我有这个列表化的文本文件:

Let's say I have this tabulated text file:

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 regexp并非自然而然。我一直在寻找类似 delete-whitespace-rectangle 之类的东西,但这并没有达到我的期望,或者我滥用了它。也希望具有按列执行此操作的能力,即:

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


推荐答案

此功能应该可以诀窍:

(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))

这篇关于如何折叠区域中的空白?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆