Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Highlighting trailing whitespace in emacs without changing character

I am trying to get emacs to highlight trailing-spaces. I have tried using 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.

Ideally I would like to just see the trailing whitespace highlighted in red without any such characters.

Disclaimer: I'm new to emacs, so this may well be obvious.

like image 844
Rohan Orton Avatar asked Dec 30 '15 14:12

Rohan Orton


1 Answers

I don't use any library. I just set show-trailing-whitespace to t and any trailing white space is shown in red. The representation of newline and space characters is NOT changed.

Actually, my ".emacs" file contains this simple line:

(setq-default show-trailing-whitespace t)

In case you don't want to edit your ".emacs" file, you may try:

  • C-h v show-trailing-whitespace RET then click the customize link
  • (or just M-x customize-variable RET show-trailing-whitespace RET)
  • Click the toggle button to set it to on (non-nil)
  • Click the menu button State > Set for Current Session
  • Click the menu button State > Save for Future Sessions

[EDIT] (thanks to Francesco Frassinelli for his comment)

With setq-default, the value is changed for every mode.

If you want to disable it for some mode (like term-mode for example), you have to:

  • find the mode name of the current buffer. Usually you can get it from within the buffer with M-x describe-mode RET (shortcut C-h m or <f1> m).

  • find the entry "hook" for this mode. Usually, it's the mode name with the suffix -hook. You can find it by searching "hook" in the buffer describing the mode. For example, you may read:

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

  • add the following to your ".emacs" file:

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

  • or you may try:

    • M-x customize-variable RET term-mode-hook RET
    • Click the INS button
    • Paste (lambda () (setq show-trailing-whitespace nil))
    • Click the menu button State > Set for Current Session
    • Click the menu button State > Save for Future Sessions

Note that show-trailing-whitespace automatically becomes buffer-local when set with setq.

like image 124
duthen Avatar answered Sep 21 '22 08:09

duthen