Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tabs in HTML mode in Emacs

Tags:

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?

like image 641
Echo says Reinstate Monica Avatar asked Feb 10 '09 14:02

Echo says Reinstate Monica


1 Answers

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

like image 157
Sean Bright Avatar answered Oct 15 '22 20:10

Sean Bright