One can use auto-mode-alist to define modes based on the regex and the filename. However I want to use different modes for files depending on which folder they are in.

So, for example, in a django project, I might want to use web-mode for *.html files, but in all other projects, I would want to use the default mode for *.html files.

How can I configure emacs to do this?

1 Answer 1


You can use auto-mode-alist to select a mode based on the entire file path, not just the name / extension. The rules are evaluated in order, so you can have an entry to match html files that match some particular parent path and then a more general rule for all other html files based only on the extension.

For example, you could add a rule to remap the html extension for anything under your projects directory:

(add-to-list 'auto-mode-alist '("projects/.*\\.html$" . web-mode))

File paths that don't match will fall through to the default html rule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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