I generally like show-trailing-whitespace. But it's distracting in shell/REPL modes, where for instance completion may leave a trailing space (e.g. ls foo TAB RET in shell-mode). Plus I'm not git- or diff-ing the contents.

What I came up with for my init.el:

;; Show trailing whitespace in most modes...
(setq-default show-trailing-whitespace t)
;; ...except I don't like that in comint-modes (shells/REPLs), because
;; completion sometimes leaving a trailing space that's distracting to
;; have highlighted, and anyway I'm not git- or diff-ing the contents.
(add-hook 'comint-mode-hook
          (lambda () (setq show-trailing-whitespace nil)))

As far as I can tell this is working fine. But I wonder if someone more-experienced might say, "The problem with doing it that way is X", or, "The idiomatic way to do that instead is Y"?

(I realize this might border on being an opinion or "best way" type of question. So I apologize in advance if it's not appropriate. But knowing the answer would help me, and maybe some other folks.)

  • 5
    Others can correct me if I'm wrong, but mode hooks are indeed the idiomatic way to make mode-specific adjustments. The only tweak I might suggest is to wrap you customizations in a named function to add to the hook rather than use an anonymous lambda -- doing so will allow you easier inspection of your hooks, and also allow you to remove your customizations later if you so choose.
    – Dan
    Jan 6 '15 at 15:36
  • If show-trailing-whitespace is provided by a global minor mode, then the minor mode usually will provide user option to exclude some cases. And yes, you can wrap show-trailing-whitespace into a global minor mode.
    – xuchunyang
    Dec 6 '18 at 15:48

Turn on show-trailing-whitespace only when it makes sense, for example, in text and programming modes:

(defun my-show-trailing-whitespace ()
  (setq show-trailing-whitespace t))

(add-hook 'text-mode-hook #'my-show-trailing-whitespace)
(add-hook 'prog-mode-hook #'my-show-trailing-whitespace)

or when you are editing files

(add-hook 'find-file-hook #'my-show-trailing-whitespace)

Wrapping show-trailing-whitespace within a global minor mode works, but the code is complicated and it slows down Emacs. I suggest using text-mode-hook/prog-mode-hook or even more specific mode hook. And your own way looks good to me as well.

(define-minor-mode show-trailing-whitespace-mode
  "Toggle `show-trailing-whitespace'."
  nil nil nil
  (setq show-trailing-whitespace show-trailing-whitespace-mode))

(define-globalized-minor-mode global-show-trailing-whitespace-mode

(defun show-trailing-whitespace-mode-on ()
  (cond ((eq (aref (buffer-name) 0) ?\s))
        ((eq major-mode 'eshell-mode))
        ((eq major-mode 'tern-mode))
        ((derived-mode-p 'comint-mode))
        (t (show-trailing-whitespace-mode))))


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.