In Emacs, intelligently call whitespace-cleanup on save
Emacs Lisp
Latest commit 6d0a351 Sep 9, 2017 @purcell purcell Add Patreon badge
Permalink
Failed to load latest commit information.
README.md Add Patreon badge Sep 9, 2017
whitespace-cleanup-mode.el Autoload customize group May 3, 2017

README.md

Melpa Status Melpa Stable Status Support me

whitespace-cleanup-mode.el

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.

Installation

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).

Usage

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