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).
11 responses so far ↓
I have the following. Not only does it say “no tabs!” but sets how many spaces to make when I hit “tab”
I also activate:
in certain language modes to point out the icky.
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:
They’re almost identical, a more elegant solution would be to have a single toggle function.
I’ve been wondering how to do this for a very long time and finally stumbled across this tip. Thank you very much.
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:
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
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.
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.
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
so how does one make emacs change it’s tab mode based on file extension?
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?
@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.
Leave a Comment