你如何删除emacs中的trailng空格?

Dav*_*iov 10 emacs

我有一个具有固定长度行的文本文件,由尾随空格填充,如:

hello world                 ?
this is some other line     ?
x                           ?
Run Code Online (Sandbox Code Playgroud)

我想删除每一行的尾随空格,看起来像

hello world?
this is some other line?
x?
Run Code Online (Sandbox Code Playgroud)

是否有可能编写一个可以解决这个问题的emacs宏?

编辑:在结尾的尾随空格之前,行可以有任意数量的空格,所以

hi     world                ?
Run Code Online (Sandbox Code Playgroud)

可以是此文件中的有效行.

vha*_*lac 36

有一个emacs命令delete-trailing-whitespace可以删除最后一个字符后面的空格.如果在没有标记任何区域的情况下运行它,它将清除整个缓冲区.如果您有活动区域,则仅清除该区域中的行.

很多人将以下代码添加到其中.emacs,因此无论何时保存文件,都会清除所有尾随空格:

(add-hook 'before-save-hook
          'delete-trailing-whitespace)
Run Code Online (Sandbox Code Playgroud)


phi*_*mue 1

Emacs 内置了fixup-whitespace( M-space),可以将多个空格缩小为只有一个空格:

\n\n
hello      world         \xe2\x86\xa9\n                    ^\n                  cursor \n\nM-x fixup-whitespace\n\nhello world \xe2\x86\xa9\n           ^\n         cursor\n
Run Code Online (Sandbox Code Playgroud)\n\n

因此,您可以简单地定义一个宏:

\n\n
    \n
  • 第一次通话fixup-whitespace
  • \n
  • 然后删除最后一个空格
  • \n
\n\n

另一种可能是M-x replace-regexp RET [ ]+' RET ' RET,它使用正则表达式解决问题。

\n