Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't change Emacs's default indentation between HTML tags

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?

like image 429
alexchenco Avatar asked Jan 16 '10 08:01

alexchenco


1 Answers

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.

like image 83
Emerick Rogul Avatar answered Nov 04 '22 21:11

Emerick Rogul