3

I want my emacs to start .php and others web files in web mode so i copied this code which is on the web-mode webpage:

(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 '("\\.[agj]sp\\'" . 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))

But it's not working. Here's my .emacs config:

(require 'package)

(add-to-list 'package-archives
         '("melpa" . "http://melpa.org/packages/")
         t)
(package-initialize)

(require 'linum)
(require 'linum-relative)
(global-linum-mode 1)
(linum-relative-on)

(require 'auto-complete)
(require 'auto-complete-config)
(ac-config-default)
(require 'yasnippet)
(yas-global-mode 1)

(set-language-environment "UTF-8")

(setq frame-title-format "Emacs")

(menu-bar-mode -1)

(tool-bar-mode -1)

(scroll-bar-mode -1)

(set-default 'cursor-type 'hbar)

(load-theme 'dracula t)


(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 '("\\.[agj]sp\\'" . 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))
3

This line is in error:

(add-to-list 'auto-mode-alist '("\\.tpl\\.php\\'" . web-mode))

It doesn't open .tpl files and .php files in web-mode, it opens files ending .tpl.php in web-mode.

Amusingly, I have that in my init file also; it came from somewhere on the web. And it doesn't work for me either.

So, to fix it, add two separate lines, one for each filetype:

(add-to-list 'auto-mode-alist '("\\.tpl\\'" . web-mode))
(add-to-list 'auto-mode-alist '("\\.php\\'" . web-mode))
  • 2
    You can also use "\\.tpl\\|\\.php\\'". – Omar Nov 1 '16 at 20:44
  • It's also done wrong on the web-mode homepage: web-mode.org – Timm Sep 15 '17 at 19:30
  • @Timm I had emailed the web-mode people, and they say that that behavior is intentional. It seems wrong to me, but I don't manage web-mode. – zck Sep 16 '17 at 1:39
0

The lines you copied associate .phtml files with web-mode. You need another couple of lines to associate .php and .co files as well.

  • The one after does .php and .tpl Normally so i don't understand what you want to say by this – Loucas Cubeddu Nov 1 '16 at 15:45
  • No, it adds files that end in .tpl.php, e.g. red.tpl.php. – icarus Nov 1 '16 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.