在不改变字符的情况下突出显示 emacs 中的尾随空格

Highlighting trailing whitespace in emacs without changing character

我正在尝试让 emacs 突出显示尾随空格。我试过使用 WhiteSpace, and also tried setting show-trailing-whitespace variable to true, but in each case it changes the representation of newline and space characters to $ and · characters, as shown in this screen capture.

理想情况下,我希望只看到以红色突出显示的尾随空格,而没有任何此类字符。

免责声明:我是 emacs 的新手,所以这很明显。

whitespace-style 变量的值更改为

(face trailing)

您可能需要重新启动 whitespace-mode 以使更改生效。

要设置一个变量,使用M-xset-variableEnter.

另一个答案是使用库 highlight-chars.el (description: Highlight library).

命令 hc-toggle-highlight-trailing-whitespace 执行您的请求。

您还可以在任何地方或在给定缓冲区中或针对给定模式自动打开此类突出显示。

我不使用任何图书馆。我只是将 show-trailing-whitespace 设置为 t,任何尾随的白色 space 都显示为红色。换行符和 space 字符的表示未更改。

实际上,我的 ".emacs" 文件包含这一行:

(setq-default show-trailing-whitespace t)

如果您不想编辑 ".emacs" 文件,您可以尝试:

  • C-h v show-trailing-whitespace RET 然后点击 customize link
  • (或 M-x customize-variable RET show-trailing-whitespace RET
  • 单击 toggle 按钮将其设置为 on (non-nil)
  • 单击菜单按钮 State > Set for Current Session
  • 单击菜单按钮 State > Save for Future Sessions

[编辑](感谢 Francesco Frassinelli 的评论)

使用 setq-default,每个模式的值都会改变。

如果您想在某些模式下禁用它(例如 term-mode),您必须:

  • 找到当前缓冲区的模式名称。通常,您可以使用 M-x describe-mode RET(快捷键 C-h m<f1> m)从缓冲区中获取它。

  • 找到此模式的条目 "hook"。通常,它是带有后缀 -hook 的模式名称。您可以通过在描述模式的缓冲区中搜索 "hook" 来找到它。例如,您可以阅读:

    Entry to this mode runs the hooks on ‘term-mode-hook’

  • 将以下内容添加到您的 ".emacs" 文件中:

    (add-hook 'term-mode-hook (lambda () (setq show-trailing-whitespace nil)))

  • 或者您可以尝试:

    • M-x customize-variable RET term-mode-hook RET
    • 单击 INS 按钮
    • 粘贴(lambda () (setq show-trailing-whitespace nil))
    • 单击菜单按钮 State > Set for Current Session
    • 单击菜单按钮 State > Save for Future Sessions

注意 show-trailing-whitespace 自动变为 buffer-local 当设置 setq 时。