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

2

6

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

Dolerite answered 8/2, 2014 at 16:32 Comment(9)
Hi, the indentation works for me, what version of web-mode are you using?Wilburn
I'm using web-mode 8.0.9 in Emacs 24.3.1Dolerite
I am using the same version, indentation works for the code <p> Lorem ipsum dolor sit amet, consectetur adipiscing elit. </p>. Does indentation fail for you even when you have only the above code in php file?Wilburn
Is that supposed to be a single line or multiple lines? It does work for a single line but not multiple. Though it seems to work in a .html file without enabling web-mode. Though not in a .php file.Dolerite
Multiple lines, just like in your question, I do not know how to post multi-line code in comments :)Wilburn
Okay, well it doesn't work.Dolerite
This is strange, it works for me, have you tried with emacs -Q if it still does not work then maybe this is a bug in web-mode, you should report it on its github repo.Wilburn
OK the indentation is not working for me now sorry the noise, actually it works for files with html extension but fails for files with php extension this is definitely a bug you should report it.Wilburn
Reported github.com/fxbois/web-mode/issues/192Dolerite
M
9

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)
Marty answered 8/7, 2015 at 1:20 Comment(0)
C
1

Could you add this

(add-to-list 'auto-mode-alist '("\\.php\\'" . web-mode))
Cryptic answered 10/2, 2014 at 12:13 Comment(1)
There doesn't seem to be any differenceDolerite

© 2022 - 2024 — McMap. All rights reserved.