How do I configure Emacs html-mode to behave like TextMate's default HTML bundle?

emacs, html, textmate

Solution

I think these settings should do the trick:

(defun my-html-mode-hook ()
  (setq tab-width 4)
  (setq indent-tabs-mode t)
  (define-key html-mode-map (kbd "<tab>") 'my-insert-tab)
  (define-key html-mode-map (kbd "C->") 'sgml-close-tag))

(defun my-insert-tab (&optional arg)
  (interactive "P")
  (insert-tab arg))

(add-hook 'html-mode-hook 'my-html-mode-hook)

An explanation of the settings in `'my-html-mode-hook` is as follows:

- set the tab width to 4

- force tabs to be inserted (as opposed to spaces)

- force the `TAB` key to insert a tab (by default it is bound to do indentation, not just insertion of tabs

- `'sgml-close-tag` is the command that inserts a close tag for you, and this setting gets you the keybinding you want

I'm having a bit of a brain freeze and couldn't figure out the simple way to have the `TAB` key insert a TAB character, so I wrote my own. I don't know why a binding to `'self-insert-command` didn't work (that's what normal keys are bound to).

The last line just adds the setup function to the `'html-mode-hook`. The key bindings really only need to be run once (as opposed to every time html-mode is enabled), but this is a little easier to read than using `'eval-after-load`. It's use is left as an exercise to the reader.

Problem

A friend of mine is considering switching to Emacs from TextMate. He is used to TextMate's default HTML editing mode which has 4-space tab stops and inserts tab characters (i.e. it does no auto-indenting by default). It also allows completion of open HTML tags with "`Cmd-Shift->`". Any ideas?

Original source