I am trying to diagnose this problem. TAB creates 4 spaces instead of a 4 col TAB like I want. But I don't think it should because C-h v indent-tabs-mode
on the buffer in question says it is set to t
. When I check my keybindings, TAB is set to c-indent-line-or-region
. Does this function ignore my tabs-mode?

- 13,558
- 11
- 40
- 58
-
Note that many people (myself included) thinks that tabs are evil: http://www.emacswiki.org/emacs/TabsAreEvil - you have changed your tab-width from the default 8 to 4. If I were to look at your code, the indention would be wrong. Furthermore, you usually like to align parts of you code, i.e. function arguments - this would not be possible with tabs alone, and you would end up with at mix of tabs and spaces. – slu May 22 '10 at 12:44
5 Answers
Tabs and indentation in Emacs is a considerably more complex subject than most people anticipate. I highly recommend spending some time reading about it -- it will almost certainly save you some confusion in the long run.
The following page at the Emacs Wiki groups together most of the relevant discussion: http://www.emacswiki.org/emacs/CategoryIndentation
There's quite a lot there, but it's worth looking through.
One or other of the TabsAreEvil and SmartTabs configurations is quite likely to be appealing to you, btw, depending on your personal opinions on the subject!
Make sure you read the page on the tab-stop-list variable. It's tucked away near the bottom of that list of links, but it's critical to understanding the behaviour of tabs in the absence of automated-indentation rules, along with things like 'tabify'.
ruler-mode is useful here as well. I enable it automatically with text-mode:
;; Use ruler in text-mode
(add-hook 'text-mode-hook
(function (lambda ()
(setq ruler-mode-show-tab-stops t)
(ruler-mode 1))))

- 71,335
- 11
- 153
- 198
I figured out the problem. It was inserting a tab character after all. It turns out I thought it wasn't because when I hit backspace that key is bound to c-electric-backspace
, which looks at the variable c-backspace-function
which was set to backward-delete-char-untabify
, which IMO defeats the purpose of having tabs.

- 13,558
- 11
- 40
- 58
-
1You may find the x-stretch-cursor variable and whitespace-mode useful for visually identifying tabs. The latter can be pretty garish by default, but it's reasonably customisable, so you may find an agreeable configuration. – phils May 25 '10 at 09:57
-
+1. If I'd seen this answer earlier I'd have saved an hour of my life. – El Zorko Jul 19 '10 at 21:06
Be sure to take a look at the first line of the file as well. If you see something like // -- tab-width: 4; Mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -- This line will override any global or mode settings.

- 11
- 1
Check tab-width
variable. If it is 8 (the default), then Emacs of course has to insert four spaces since a tab would be "too much".
-
It was 8. I changed it to 4 just now, yet the problem persists. – Fletcher Moore May 21 '10 at 21:01
Check of the file for Emacs "File Local Variables". These specially formatted lines can override your settings when that file is loaded.
Here is an example from the bottom of a bit of Ruby code, forcing indent to 2 spaces, and tabs converted to spaces:
# Local Variables:
# tab-width: 2
# ruby-indent-level: 2
# indent-tabs-mode: nil
# End:

- 103,207
- 26
- 155
- 191