M-x all-things-emacs

Quick Tip: Spaces instead of Tabs

September 30th, 2007 by Ryan McGeary · 20 Comments

Tab characters used as indentation of source code is a pet peeve of mine. Add this to your emacs initialization to make sure all indentation uses spaces instead.

;; I hate tabs!
(setq-default indent-tabs-mode nil)

Now, if you also use tab completion everywhere, someday, you’ll want to actually insert a real <tab> character (ASCII 9), but won’t be able too. Quoted-insert to the rescue. Type C-q C-i to insert a horizontal tab character.

Note: Even though, I’ve aired my religious preference on this topic, my intention is not to start a war but to teach those who like spaces how to configure emacs (Yes, I’ve read the heated material on the subject).

Tags: newbie · quick · tips

20 responses so far ↓

  • 1 MikeH // Oct 1, 2007 at 2:05 pm

    I have the following. Not only does it say “no tabs!” but sets how many spaces to make when I hit “tab”

    ;; to setup tabs
    (setq c-basic-indent 2)
    (setq tab-width 4)
    (setq indent-tabs-mode nil)
  • 2 zenspider // Oct 8, 2007 at 8:10 am

    I also activate:


    in certain language modes to point out the icky.

  • 3 Bill // Jan 8, 2008 at 5:11 pm

    Great blog! Saves me the trouble of starting an emacs blog.

    Sometimes I want to see the hated tabs in a file with a ^I. Here are a couple functions to turn that on and off:

    (defun my-show-tabs ()
      (let ((i 0) (disptab make-display-table)))
        (while (&lt; i 32)
          (or (= i ?\n)
              (aset disptab i (vector ?^ (+ i 64))))
          (setq i (1+ i)))
        (aset disptab 127 (vector ?^ ??))
        (setq buffer-display-table disptab)))
    (defun my-hide-tabs ()
      (let ((i 0) (disptab make-display-table)))
        (while (&lt; i 32)
          (or (= i ?\n) (= i ?\t)
              (aset disptab i (vector ?^ (+ i 64))))
          (setq i (1+ i)))
        (aset disptab 127 (vector ?^ ??))
        (setq buffer-display-table disptab)))

    They’re almost identical, a more elegant solution would be to have a single toggle function.

  • 4 Layne // Jan 20, 2008 at 4:43 am

    I’ve been wondering how to do this for a very long time and finally stumbled across this tip. Thank you very much.

  • 5 Charles Hethcoat // Mar 31, 2008 at 9:09 pm

    It looks like actual tabs are disliked by programmers writing source code. I can see the problem.

    But what about data, the kind that goes as input to analysis programs? Putting tabs between number fields in a big file of numbers works for me. It is easy to see, easy to edit, easy to sed/grep/awk, easy to plot, easy to copy in and out of spreadsheets. On my Windows system, tab-separated data fields are intrinsically compatible with Excel, Gnuplot, and Emacs, both coming and going. When I transfer them to Linux or AIX or whatever, they work the same. My co-workers are sometimes amazed at how quickly I can produce a publication-quality plot from an Excel spreadsheet, tabs and all.

    I use the following convention in my data files religiously:

    # These are comments...
    # Federal Minimum Wage Rates, 1955-2003
    # 1. Adjusted for inflation using the CPI-U (Consumer Price Index for
    # All Urban Consumers).
    # Source: Web: www.dol.gov/esa/public/minwage.
    #Year	Current	Constant
    #            Dollars	(1996)
    #                              Dollars
    1955	0.75	4.39
    1956	1.00	5.77
    1957	1.00	5.58
    1958	1.00	5.43
    1959	1.00	5.39
    1960	1.00	5.30
    1961	1.15	6.03

    I pasted the data into your text box with real tabs, not spaces; I am curious how they will look on your blog. It might show the only downside for me, which is that pasting tabs into some proportional word processors causes inconsistent spacing. In that case, I usually make a copy, convert the tabs to spaces, and keep the Courier typeface, which maintains the layout. But I keep my original data, with its tabs, intact at all times.

    Charles Hethcoat

  • 6 Charles Hethcoat // Mar 31, 2008 at 9:11 pm

    It’s me again. It looks like the comments were messed up on the blog. Just to be clear, they are typical Unix-style hash marks, one per line; they didn’t look that way. But the data did seem to line up nicely, and that’s what I’m talking about.

  • 7 Ryan McGeary // Mar 31, 2008 at 10:42 pm

    Charles, Hi. I fixed the formatting in your original post. If it still doesn’t look like you expected it to, my guess is that it’s a difference between tab stops for the browser and what you have set in emacs.

  • 8 Charles Hethcoat // May 30, 2008 at 3:12 pm

    Understood. I sometimes pad fields with a few spaces to make them line up visually on-screen. The comments look pretty good now, just slightly misaligned on my browser, but it isn’t worth making another run at it. The takeaway lesson is that hard tabs are not 100% evil. They have their uses.


    Charlie Hethcoat

  • 9 tim // Apr 15, 2009 at 10:32 pm

    so how does one make emacs change it’s tab mode based on file extension?

  • 10 L.Guruprasad // Nov 17, 2009 at 7:04 am

    I am able to make emacs insert spaces when I hit a tab, by using the above mentioned customization. But the tab width isn’t changing to 4 spaces though the .emacs file says so. I even tried ‘M-x set-variable’ to set the tab-width but pressing the tab key always inserts 8 spaces. Any idea where I could be going wrong?

  • 11 Gary Johnson // Jun 13, 2010 at 8:29 pm

    @L. Guruprasad

    (setq tab-width 4) is buffer local even when placed in your .emacs file. That means the .emacs file uses a tab-width of 4, but everything else remains at 8. To set this globally, press C-h-v tab-width, and then customize the variable via the link in the Help window.

  • 12 nigel // Nov 8, 2012 at 2:39 pm

    And what about us groff users where tab is the default separator for items in tbl?

  • 13 Emacs: How to use white spaces instead of tabs when writing C or Vala code | ewgeny // May 9, 2013 at 6:52 pm

    [...] Additional references: spaces instead of tabs [...]

  • 14 bestcarspeakerssubwoofers.com // Jul 1, 2015 at 7:46 pm

    ... [Trackback]

    [...] Read More: emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/ [...]

  • 15 pocket door installation // Jul 2, 2015 at 3:08 am

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/ [...]

  • 16 www.fullcontracting.com // Jul 2, 2015 at 3:58 am

    ... [Trackback]

    [...] There you will find 98614 more Infos: emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/ [...]

  • 17 inner circle trading // Jul 2, 2015 at 10:57 am

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/ [...]

  • 18 training dentist // Jul 2, 2015 at 9:51 pm

    ... [Trackback]

    [...] Read More here: emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/ [...]

  • 19 visit Medicine Today // Jul 2, 2015 at 11:00 pm

    ... [Trackback]

    [...] Informations on that Topic: emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/ [...]

  • 20 pest control in london // Jul 3, 2015 at 10:17 pm

    ... [Trackback]

    [...] There you will find 49417 more Infos: emacsblog.org/2007/09/30/quick-tip-spaces-instead-of-tabs/ [...]