views:

135

answers:

4

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?

+1  A: 

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".

doublep
It was 8. I changed it to 4 just now, yet the problem persists.
Fletcher Moore
+1  A: 

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:
Wayne Conrad
+2  A: 

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.

Fletcher Moore
You 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
+1. If I'd seen this answer earlier I'd have saved an hour of my life.
El Zorko
+2  A: 

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))))
phils