In some buffers (e.g. diff buffers) the trailing whitespace is essential to the format and must be preserved. What is causing Emacs to invoke delete-trailing-whitespace whenever I save the buffer?

  • The before-save-hook contains only (time-stamp), which doesn't alter trailing white space.

  • The variables write-contents-functions, write-file-functions, local-write-file-hooks all have the value nil.

  • The active minor modes are, according to describe-mode:

    Async-Bytecomp-Package Auto-Composition Auto-Compression Auto-Encryption Auto-Revert Column-Number Diff-Auto-Refine File-Name-Shadow Font-Lock Global-Eldoc Global-Font-Lock Global-Git-Commit Line-Number Magit-Auto-Revert Midnight Mouse-Wheel Projectile Projectile-Global Shell-Dirtrack Tool-Bar Tooltip Transient-Mark

    None of those have any obvious behaviour to remove trailing whitespace on save.

  • This unwanted behaviour still happens even when I manually set fundamental mode in the buffer.

The backtrace when the delete-trailing-whitespace function gets called looks like:

* delete-trailing-whitespace()
* run-hook-with-args-until-success(delete-trailing-whitespace)
* basic-save-buffer(t)

I haven't asked for delete-trailing-whitespace, it's not in any relevant hooks that I can find, yet run-hook-with-args-until-success is being told to run it. Why?

  • 2
    Does it happen when you start Emacs without your init file (emacs -Q)? If not, recursively bisect your init file until you isolate the code that deals with the whitespace. – Dan Sep 8 '17 at 11:59
  • 1
    What @Dan said. Or M-x debug-on-entry save-buffers-kill-terminal and step through the debugger to see why it happens. – Drew Sep 8 '17 at 13:16
  • 1
    The functions hooked into before-save-buffer are not called by run-hook-with-args-until-success but the functions in the following hooks are: write-contents-functions, local-write-file-hooks, and write-file-functions. Furthermore, there is a discussion to use delete-trailing-whitespace for git there: stackoverflow.com/questions/591923/… and there: stackoverflow.com/questions/14164292/… using write-file-functions. Search for that! – Tobias Sep 9 '17 at 18:38

Your Answer

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

Browse other questions tagged or ask your own question.