Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs indentation for html (web-mode) doesn't work properly

I'm using web-mode in Emacs to get syntax highlighting and indentation for PHP and HTML.

If I have this code in a .php file

<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>

And then put the cursor on the middle line and press tab then nothing happens.

I want it to look like this:

<p>
     Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>

If I put the text in a tag on a single line and try to indent, it works.

This:

<p>
<a>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</a>
</p>

turns into this, which it should

<p>
    <a>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</a>
</p>

My .emacs file

(require 'web-mode)
(add-to-list 'auto-mode-alist '("\\.phtml\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.jsp\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.as[cp]x\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.erb\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.mustache\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.djhtml\\'" . web-mode))

(setq web-mode-markup-indent-offset 4)
(setq web-mode-css-indent-offset 4)
(setq web-mode-code-indent-offset 4)
(setq web-mode-indent-style 4)

enter image description here

like image 494
Oskar Persson Avatar asked Feb 08 '14 16:02

Oskar Persson


2 Answers

try put these setting in a hook function:

(defun my-web-mode-hook ()
  "Hooks for Web mode."
    (setq web-mode-markup-indent-offset 4)
    (setq web-mode-css-indent-offset 4)
    (setq web-mode-code-indent-offset 4)
    (setq web-mode-indent-style 4)
)
(add-hook 'web-mode-hook  'my-web-mode-hook)
like image 175
taxus Avatar answered Nov 05 '22 22:11

taxus


Could you add this

(add-to-list 'auto-mode-alist '("\\.php\\'" . web-mode))
like image 29
fxbois Avatar answered Nov 05 '22 22:11

fxbois