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)

Comments (2)

1. chris dixon wrote:

If everyone were to know about their whitespace, life would be a lot more depressing.

Posted on 25 September 2009 at 9:09 PM  |  permalink

2. peter wrote:

Haha, it's a cruel world. Btw, using ctrl+j to go to new lines instead of enter will not only maintain proper tabbing, but it will also strip off any trailing whitespace from your current line—sometimes preventitive measures are the best :)

Posted on 26 September 2009 at 10:09 AM  |  permalink

Did you find this helpful or fun? paypal.me/mrcoles

Peter Coles

Peter Coles

is a software engineer living in NYC who is building Superset 💪 and also created GoFullPage 📸
more »

github · soundcloud · @lethys · rss