M-x all-things-emacs

Indent Whole Buffer

January 17th, 2007 by Rob Christie · 50 Comments

I use this function almost daily. Why? Well, it’s because too many developers use editors that muck the alignment of the source.

1
2
3
4
5
6
(defun iwb ()
  "indent whole buffer"
  (interactive)
  (delete-trailing-whitespace)
  (indent-region (point-min) (point-max) nil)
  (untabify (point-min) (point-max)))

Open a file, M-x iwb, save, commit, and edit happily.

I can’t take credit for it, but it seems like it should be passed along. I think I originally copied it from another co-worker’s .emacs file when I started using emacs.

Tags: elisp · tips

50 responses so far ↓

  • 1 Jake // Nov 15, 2007 at 10:33 pm

    that’s cool.
    i’ve tried to write one,but with no luck.

    and here you iwb() is.

    thanks~

  • 2 Doug // Sep 25, 2008 at 9:59 pm

    This is great, thanks very much for sharing this. I’ve wanted a function like this for quite some time now, and haven’t had great luck writing my own. Thanks!

  • 3 vedang // Oct 8, 2008 at 7:16 am

    Thanks a lot! I don’t know Lisp, I mostly work with C/C++. Much of the code I come across is poorly indented, and I have to sit indenting the code myself / using the indent-region feature (which, though it makes the job easier, still involves me selecting the region!).
    So, this is going to be a big help to me!

  • 4 konsty // Feb 5, 2009 at 8:35 pm

    Thanks.
    Simply useful :)

  • 5 Steve // Mar 31, 2009 at 5:22 pm

    I took your awesome definition and made it even awesomer by adding the following to the end:

    (global-set-key [f12] ‘iwb)

    Woot! This was exactly what I wanted.

  • 6 Deniz Dogan // Dec 9, 2010 at 1:24 pm

    I prefer binding these sorts of things to mnemonics. I have this bound to “C-c i b”.

  • 7 Yichao Yu // Jul 5, 2012 at 4:56 pm

    Thanks for the function
    One minor problem: u might want to disable the untabify for Makefile…

  • 8 Luis Villegas // Oct 10, 2012 at 12:55 am

    Awesome!, thanks for sharing! I really love this function, I was tired of hitting TAB and TAB as an asshole. Thanks again!

  • 9 Derek V // Feb 8, 2013 at 5:15 pm

    Funny, I was looking something up to write this exact function.

  • 10 Amanda // Aug 15, 2013 at 6:22 pm

    Cool! Thanks! Exactly what I was looking for. And I hadn’t heard of a .emacs file. Guess I can add the meta-x butterflies command.

  • 11 Engagement rings // Jul 22, 2015 at 12:35 am

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 12 Personal Trainer Seattle // Jul 22, 2015 at 12:39 am

    ... [Trackback]

    [...] There you will find 28724 more Infos: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 13 Con-Cret Pump Extreme // Jul 23, 2015 at 4:23 pm

    ... [Trackback]

    [...] There you will find 95351 more Infos: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 14 mission impossible 5 telecharger // Jul 24, 2015 at 8:27 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 15 amazin gas station video // Jul 24, 2015 at 8:58 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 16 amazin gas station video // Jul 24, 2015 at 10:31 am

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 17 clique aqui // Jul 24, 2015 at 7:43 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 18 dui defense attorney // Jul 24, 2015 at 9:30 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 19 here // Jul 25, 2015 at 2:10 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 20 locksmith granite bay ca // Jul 26, 2015 at 6:19 am

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 21 Fair Oaks Locksmith // Jul 26, 2015 at 6:26 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 22 should i increase my propecia dosage // Jul 27, 2015 at 9:06 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 23 Cartersville Web Design // Jul 27, 2015 at 2:58 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 24 deep cleaning service London // Jul 27, 2015 at 6:26 pm

    ... [Trackback]

    [...] There you will find 86667 more Infos: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 25 surf store // Jul 28, 2015 at 6:47 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 26 Insider Binary Bot // Jul 31, 2015 at 3:46 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 27 chiropractors in Omaha // Jul 31, 2015 at 6:26 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 28 Prodaja staklene ambalaze // Aug 1, 2015 at 4:55 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 29 security training // Aug 1, 2015 at 2:34 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 30 www.tiptopplumbers.com // Aug 2, 2015 at 8:16 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 31 rock landscaping ideas // Aug 2, 2015 at 8:24 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 32 blackhatlinks social signals social Signals // Aug 2, 2015 at 10:27 pm

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 33 rain gutter service // Aug 2, 2015 at 11:40 pm

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 34 http://www.tiptoproofers.net // Aug 2, 2015 at 11:41 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 35 sale ventolin // Aug 3, 2015 at 1:54 pm

    ... [Trackback]

    [...] There you will find 69681 more Infos: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 36 hullairporttransfers.co.uk taxi from hull to doncaster airport // Aug 3, 2015 at 11:39 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 37 rheem furnace repair // Aug 4, 2015 at 1:06 am

    ... [Trackback]

    [...] Find More Informations here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 38 http://www.tiptopglassrepair.com // Aug 4, 2015 at 1:26 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 39 click resources // Aug 4, 2015 at 2:16 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 40 Do I need a sub-top under my stone countertops? // Aug 4, 2015 at 2:42 am

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 41 http://selectcontractors.net // Aug 5, 2015 at 7:26 am

    ... [Trackback]

    [...] There you will find 47672 more Infos: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 42 How many “convenience” outlets should be installed in each room? // Aug 5, 2015 at 10:41 am

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 43 buy social signals // Aug 5, 2015 at 11:14 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 44 dolls // Aug 7, 2015 at 12:40 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 45 weblink // Aug 7, 2015 at 7:18 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 46 Maryland Search // Aug 9, 2015 at 6:22 pm

    ... [Trackback]

    [...] Read More Infos here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 47 visit the site // Aug 9, 2015 at 6:46 pm

    ... [Trackback]

    [...] There you will find 24916 more Infos: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 48 ttt4 // Aug 10, 2015 at 10:11 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 49 mahjong gratuit // Aug 11, 2015 at 12:01 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]

  • 50 Social Signal tool // Aug 12, 2015 at 10:11 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/01/17/indent-whole-buffer/ [...]