Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use php mode for .php files, but codeigniter uses .php for the template files which are mostly html, so I would like to use html-mode for those files.

Is there a way to say in the .emacs file "If the file path contains '/views/' use html-mode"?

share|improve this question
up vote 1 down vote accepted

If you want to use html-mode for all files under a directory named views and its sub-directories, you can add the following line at the end of .emacs:

(add-to-list 'auto-mode-alist '("/views/" . html-mode))

Note that ("/views/" . html-mode) should appear before an entry for .php files when you check the value of auto-mode-alist by C-hvauto-mode-alist.

Similarly, the following line at the end of .emacs will make Emacs choose html-mode for all .php files under a directory named views and its sub-directories:

(add-to-list 'auto-mode-alist '("/views/.*\\.php\\'" . html-mode))

From auto-mode-alist's documentation:


Alist of filename patterns vs corresponding major mode functions. Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL). ... Visiting a file whose name matches REGEXP specifies FUNCTION as the mode function to use. FUNCTION will be called, unless it is nil.

share|improve this answer
That worked like a charm. Thank you! – RobKohr Sep 27 '12 at 13:05

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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