Tabs in HTML mode in Emacs

I'm a tab person. Unless I'm working on a project that is already using spaces, I use tabs. I mostly do php and html work. I've got tabs in php working well. But I can't figure out how to have html mode use tabs instead of two spaces.

Here is what I have so far:

(setq c-default-style "python")
(setq-default c-basic-offset 4
              tab-width 4
              indent-tabs-mode t)

What can I set so that html mode will use tabs?

Upvotes: 12

Views: 5274

Answers (2)

Sean Bright
Sean Bright

Reputation: 120714

(add-hook 'html-mode-hook
          (lambda()
            (setq sgml-basic-offset 4)
            (setq indent-tabs-mode t)))

This works because when indent-tabs-mode is t, Emacs' default indentation logic replaces spaces with tabs whenever it can while still preserving the proper column offset for indentation. So for example if your code is supposed to be offset by four characters per indentation level (i.e. sgml-basic-offset is 4, as above), your code should be indented two levels deep, and there are four columns per indentation level, then Emacs calculates that if it indents with two tabs and zero spaces, that will result in the proper column offset.

This also means, however, that the above won't work quite right if you've messed with your tab-width. For example if you set it to 8 and are indenting one level deep, Emacs calculates that even if it inserts just a single tab, the visual column offset (8) will be bigger than the desired offset (4). So it'll insert four spaces instead. Try setting sgml-basic-offset to the same thing as your tab-width.

Upvotes: 29

Brian Postow
Brian Postow

Reputation: 12217

actually, indent-tabs-mode should probably be t by default.

Try M-i, just for a lark. That should insert a tab character. It's not a GREAT Solution, but it might work in a pinch.

ALSO, how did you test for space vs tab? moving across it? or deleting it? you might have 'backward-delete-char-untabify' rearing it's head.

Upvotes: 2

Related Questions