Skip to content
In Emacs, intelligently call whitespace-cleanup on save
Branch: master
Clone or download
purcell Merge pull request #13 from jrosdahl/dont-set-global-values
Don’t set global values of indent-tabs-mode and whitespace-style
Latest commit 7242714 Jan 6, 2019
Type Name Latest commit message Commit time
Failed to load latest commit information. Add Patreon badge Sep 9, 2017
whitespace-cleanup-mode.el Don’t set global values of indent-tabs-mode and whitespace-style Jan 6, 2019

Melpa Status Melpa Stable Status Support me


This Emacs library minor mode will intelligently call whitespace-cleanup before buffers are saved.

whitespace-cleanup is a handy function, but putting it in before-save-hook for every buffer is overkill, and causes messy diffs when editing third-party code that did not initially have clean whitespace.

Additionally, whitespace preferences are often project-specific, and it's inconvenient to set up before-save-hook in a .dir-locals.el file.

whitespace-cleanup-mode is a minor mode which calls whitespace-cleanup before saving the current buffer, but only if the whitespace in the buffer was initially clean. It determines this by quickly checking to see if whitespace-cleanup would have any effect on the buffer.


If you choose not to use one of the convenient packages in Melpa, you'll need to add the directory containing whitespace-cleanup-mode.el to your load-path, and then (require 'whitespace-cleanup-mode).


Enable whitespace-cleanup-mode in an individual buffer like this:

M-x whitespace-cleanup-mode

Optionally enable it everywhere by default using global-whitespace-cleanup-mode. (You can override that by setting whitespace-cleanup-mode to nil in file or directory local variables.)

Alternatively, enable whitespace cleanup for a particular major mode:

(add-hook 'ruby-mode-hook 'whitespace-cleanup-mode)

To enable it for an entire project, set whitespace-cleanup-mode to t in your .dir-locals.el file.

This mode is built upon some functionality built into whitespace-mode, namely whitespace-action: if you would rather see a warning when saving a file with bogus whitespace, or even have the save aborted, then set that variable.

Steve Purcell's blog // @sanityinc on Twitter

You can’t perform that action at this time.