25 September 2009
Emacs - OCD about whitespace?
When writing code, do you ever find yourself needlessly formatting spaces between functions, lining things up, and removing trailing whitespace? Well, if you code in emacs and actually want to pursue these lofty goals, I can help you target the last one. Add this to your .emacs file (located in ~/.emacs).
;; show trailing whitespace (setq-default show-trailing-whitespace t)
All of a sudden, seemingly well formatted code will reveal offensive whitespace—then again, maybe ignorance is bliss?
Notice how Ben Bitdiddle is not just bad at writing python code, but he also leaves a bunch of trailing whitespace.
Update: my friend Ben (not Ben Bitdiddle) pointed out that you can use the command:
M-x delete-trailing-whitespace to remove any trailing whitespace from a file. This reminded me that Emacs can be customized to do almost anything, which led to a much easier solution—add the following to your .emacs file and whitepspace will be stripped before any saves:
(add-hook 'before-save-hook 'delete-trailing-whitespace)