Doing Tabs In Emacs


As you may know I like using the emacs editor (I use vi too - so please no wars). In general I love the key bindings and the elegant power built into the editor. One of the things that bothers me about it though is it tries to help too much when I hit tab. When I press the tab key, I want a tab. That’s why it’s the tab key.

I don’t want spaces that pretend to be tabs, and I don’t want a combination of spaces and tab characters that supposedly line up my code better; I just want a tab. To be a bit cynical, when I press the letter t, does it produce “tee” in the editor; no, it just makes a “t” - because it’s the t key.

I’ve always just kind of sighed and kept going in the past, but I decided to try to find a way to make emacs add a tab when I press the tab key. After a bit of searching, I found a few commands you can add to your ~/.emacs or ~/Library/Preferences/Aquamacs Emacs/customizations.el file to get a tab to be a tab:

(global-set-key (kbd "TAB") 'self-insert-command)
(global-set-key (kbd "TAB") 'tab-to-tab-stop)
(setq-default tab-width 4)
(setq-default tab-stop-list (list 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 \
64 68 72 76 80 84 88 92 96 100 104 108))

The default tab width on emacs is 8 (which is huge), so this sets it to 4. If you prefer a different size change the last 2 commands to reflect the different size.

One draw back to this is you lose auto indent. My lisp skills are more-or-less zero, so if anyone knows how to do “get the indention of the previous line and add that to the beginning of a new line” in emacspeak I’d love to know how to do that. While that’s not the most optimal indention scheme, it works 90% of the time for me.