假设我有这个列表文本文件:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
Run Code Online (Sandbox Code Playgroud)
我怎样才能将其转换为:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
Run Code Online (Sandbox Code Playgroud)
我知道我可以replace-regexp在该地区使用,但Emacs regexps并不是自然而然的.我一直在寻找类似的东西delete-whitespace-rectangle,但这并不符合我的期望,或者我在滥用它.能够在每列中执行此操作也是可取的,即:
field1 variable_length_field variable_length_field
aaaaaa aaaa aaaaaaaaa
bbbbbb bbbbbbbbbbbbbbbbbbbb bbbb
Run Code Online (Sandbox Code Playgroud)
Tre*_*son 14
这个功能应该可以解决问题:
(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 " ")))))
Run Code Online (Sandbox Code Playgroud)
并且,由于问题已更新以应用于矩形中的空格,请尝试以下操作:
(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))
Run Code Online (Sandbox Code Playgroud)
小智 12
没有真正回答你的问题,但有
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]
Run Code Online (Sandbox Code Playgroud)
当你想在一个关闭案例中删除空格时这很有用.它可能适用于宏的情况,其中删除是在没有固定模式的随机行.