views:

194

answers:

1

I'm confused about the Emacs indentation paradigm.

I have this in my .emacs file:

(setq-default tab-width 4)

If I press TAB in the following situation

                    <ul>
(caret)
                    </ul>

it end up like this

                   <ul>
                     (caret)
                   </ul>

(with 2 spaces indentation between the HTML tags.)

It should end up like this:

                   <ul>
                       (caret)
                   </ul>

I tried everything:

(setq-default tab-width 4)
(setq-default indent-tabs-mode t)
(setq tab-stop-list '(4 8 12 16))

I've set every possible Emacs setting about indentation to 4 but that 2 space indentation is still there.

Any suggestions?

+9  A: 

Setting the tab width isn't applicable in this scenario, but I understand your confusion; Emacs provides several tab-related variables and determining the correct one for a particular scenario can be confusing.

This EmacsWiki article provides more details about setting the indentation level for HTML; in general, EmacsWiki is a great resource for Emacs tips.

In this specific case, since you're using Emacs' standard HTML mode (html-mode, as defined by sgml-mode), the variable that you want to set is sgml-basic-offset. That variable defaults to 2, but you can change it to 4 as follows:

(setq sgml-basic-offset 4)

To make this change specific only to html-mode, you can use the following code:

(add-hook 'html-mode-hook
  (lambda ()
    ;; Default indentation is usually 2 spaces, changing to 4.
    (set (make-local-variable 'sgml-basic-offset) 4)))

This all assumes that you're using Emacs 22 or later; if that's not the case, the EmacsWiki page that I linked to contains a workaround for earlier versions of Emacs.

Emerick Rogul
It worked thanks! I'm curious why not:(setq html-basic-offset 4)?
janoChen
HTML is a derivative of SGML. The Emacs functionality for `html-mode` is provided by a general-purpose SGML package, which is why the variable is `sgml-basic-offset`. This is why it's better to set that variable in the `html-mode-hook`; that way, it will only affect HTML files, and not SGML files in general.
Emerick Rogul
This would depend on which mode you're using to edit your HTML code. There's no real *default* as such.
Noufal Ibrahim