M-x all-things-emacs

Quick Tip: Spaces instead of Tabs

September 30th, 2007 by Ryan McGeary · 43 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

43 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:

        (highlight-tabs)
        (highlight-trailing-whitespace)

    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 ()
      (interactive)
      (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 ()
      (interactive)
      (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
    ...(etc)
    

    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.

    Cheers,
    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.

    Thanks.

    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

    Hi,
    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 victoriajam // Jul 30, 2015 at 9:03 pm

    victoriajam.ru

    victoriajam.ru

  • 15 smotretonline // Jul 30, 2015 at 10:29 pm

    http://smotretonline2015.ru/

    smotretonline

  • 16 victoriajam // Jul 30, 2015 at 11:33 pm

    http://victoriajam.ru.websitetrafficspy.com/

    victoriajam

  • 17 aran.com.ua // Jul 31, 2015 at 10:16 am

    Ñòðîèòåëüíûå ìàòåðèàëû

    Ñòðîèòåëüíûå ìàòåðèàëû

  • 18 ìñ÷ìÿ÷ñì // Jul 31, 2015 at 1:56 pm

    Ëó÷øåå âèäåî èíòèì äëÿ âçðîñëûõ

    Ëó÷øåå âèäåî èíòèì äëÿ âçðîñëûõ http://links-free.ru/free/

  • 19 Ëó÷øåå âèäåî èíòèì äëÿ âçðîñëûõ // Jul 31, 2015 at 10:21 pm

    Ëó÷øåå âèäåî èíòèì äëÿ âçðîñëûõ

    Ëó÷øåå âèäåî èíòèì äëÿ âçðîñëûõ http://n90553t6.bget.ru/

  • 20 "Ñìîòðèòåëü" íîâûé ðîìàí // Aug 3, 2015 at 3:46 pm

    “Ñìîòðèòåëü” Âèêòîð Ïåëåâèí

    “Ñìîòðèòåëü” Âèêòîð Ïåëåâèí íîâûé ðîìàí

  • 21 Ïñèõîëîã îíëàéí. Ïîìîùü ïñèõîëîãà. // Aug 8, 2015 at 2:34 pm

    Ïñèõîëîã îíëàéí. Ïîìîùü ïñèõîëîãà.

    Ïñèõîëîã îíëàéí. Ïîìîùü ïñèõîëîãà. http://batmanapollo.ru/

  • 22 Cinemaholics // Aug 8, 2015 at 8:42 pm

    MODA FASHION BEAUTY

    NEWS HISTORY

  • 23 MODA FASHION BEAUTY // Aug 8, 2015 at 8:49 pm

    NEWS HISTORY

    NEWS HISTORY cinemaholics

  • 24 forum psy // Aug 8, 2015 at 9:07 pm

    forum

    Ïñèõîëîãèÿ aran.com.ua/forum

  • 25 forum psy // Aug 8, 2015 at 9:18 pm

    forum help psy

    Ïñèõîëîãèÿ aran.com.ua/forum

  • 26 forum psy // Aug 8, 2015 at 9:54 pm

    psyhelp

    Ïñèõîëîãèÿ aran.com.ua/forum

  • 27 X-art // Aug 8, 2015 at 10:46 pm

    X-art

    X-art

  • 28 LUGANSK // Aug 9, 2015 at 11:04 am

    LUGANSK

    LUGANSK

  • 29 dekorativnye-poly // Aug 9, 2015 at 12:58 pm

    polprom

    dekorativnye-poly

  • 30 bert hellinger // Aug 12, 2015 at 4:03 pm

    bert hellinger

    bert hellinger

  • 31 vtope // Aug 12, 2015 at 4:08 pm

    vtope

    vtope

  • 32 SOCKS-5 // Aug 12, 2015 at 5:35 pm

    SOCKS-5

    SOCKS-5

  • 33 HD VIDEO 24/7 // Aug 12, 2015 at 6:36 pm

    HD VIDEO 24/7

    HD VIDEO 24/7

  • 34 HD FILMS // Aug 15, 2015 at 10:46 pm

    HD FILMS

    HD FILMS

  • 35 Mission: Impossible ? Rogue Nation // Aug 15, 2015 at 11:26 pm

    Mission: Impossible ? Rogue Nation

    Mission: Impossible ? Rogue Nation

  • 36 online credit // Aug 22, 2015 at 2:22 pm

    online credit

    online credit

  • 37 Video fraiche // Aug 30, 2015 at 4:44 pm

    Coolest video L’?il

    Ceci est le plus cool de la video que vous voyez aujourd’hui

  • 38 Video fraiche // Aug 30, 2015 at 4:51 pm

    La video la plus froide en 2015

    Coolest video L’?il

  • 39 french-art-deco // Aug 30, 2015 at 7:51 pm

    french-art-deco

    french-art-deco

  • 40 French art deco table // Aug 30, 2015 at 7:57 pm

    French art deco table

    French art deco table

  • 41 French art deco design // Aug 30, 2015 at 8:02 pm

    French art deco design

    French art deco design

  • 42 French modern art deco graphic design // Aug 30, 2015 at 8:07 pm

    French modern art deco graphic design

    French modern art deco graphic design

  • 43 French art deco ironwork designs // Aug 30, 2015 at 8:20 pm

    French art deco ironwork designs

    French art deco ironwork designs