I once had emacs trim trailing whitespace upon leaving a line, if that line had been modified.

Note: I already have trim at save, it is not what I am asking for here. I have the package ws-butler, but it seems to only act on save.

It was working with my previous config in emacs 26, but I cannot see a package other than ws-butler there, I suspect it might be some built-in I fail to configure?

What could possibly have been doing that trimming?

  • 1
    Maybe ws-trim.el(search for ws-trim on the page)? The canonical distribution was hosted on the lysator FTP server. I found a copy at github.com/windley/emacs/blob/master/ws-trim.el however.
    – NickD
    Oct 17, 2022 at 13:30
  • did you have a hook calling delete-trailing-whitespace?
    – nega
    Oct 17, 2022 at 13:57
  • @NickD: you got it, thanks!! I even had a copy of ws-trim.el in my old config repo, although the setting up of it was gone from the init.el.
    – Gauthier
    Oct 18, 2022 at 10:50
  • @NickD If you want to make an answer of your comment, I'd gladly give you the internet points.
    – Gauthier
    Oct 18, 2022 at 10:50
  • Glad that the ws-trim mention helped, but I'd rather you provided an answer that describes how you use it to answer your question. Just mentioning ws-trim was easy but I would have to do significant amounts of research to provide a good answer and I won't have the time to do that any time soon - if ever !-)
    – NickD
    Oct 18, 2022 at 12:53

1 Answer 1


Thanks to @NickD for finding the package I was missing: ws-trim.

It is mentioned here, which is where I found it in the first place, long ago.

Download the source, put it somewhere in emacs load-path (I have (add-to-list 'load-path "~/.emacs.d/includes") in my init).

The following gives me the minor mode with the behavior I am looking for:

;; ws-trim trims trailing whitespace at navigating from line.
(require 'ws-trim)
(setq ws-trim-level 1) ; 1 -> only modified lines are trimmed.
(setq ws-trim-method-hook '(ws-trim-trailing))
  • 1
    One nit: it is preferable to use add-hook to modify a hook: for one, it checks for duplicates, thereby avoiding problems down the line; also, if it has some functions on it already, the setq will get rid of them - that might be a big problem. It is not much of a problem here (presumably, nobody else - some library or package - would want to add anything to it), but it is a good habit to get into. Just replace the last setq with (add-hook 'ws-trim-method-hook #'ws-trim-trailing).
    – NickD
    Oct 19, 2022 at 1:24
  • 1
    @NickD The lib does this: ;;;###autoload (defvar ws-trim-method-hook '(ws-trim-leading ws-trim-trailing)). Isn't add-hook going to just add to the list? I don't want to trim leading, so I thought the hook needed replacing?
    – Gauthier
    Oct 19, 2022 at 7:45
  • You are right that you need to replace it. It's unfortunate that the library does that initialization (IMO). I would rather have it give you an empty hook that users can populate as they want. In this case, I would add a (defvar ws-trim-method-hook nil) before ws-trim.el is loaded to enforce my preference and then use add-hook in my regular way, but it is a minor nit.
    – NickD
    Oct 19, 2022 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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