;;; /tmp/test/.dir-locals.el
((auto-mode-alist . (("/\\.dir-locals\\(-2\\)?\\.el\\'" .  lisp-data-mode)
                     ("/[^/-][^/]*\\.el\\'"             . emacs-lisp-mode))))

I use emacs -Q to visit file /tmp/test/.dir-locals.el, then Emacs chooses emacs-lisp-mode for this file.

What goes wrong? Why not lisp-data-mode?

GNU Emacs Lisp Reference Manual says that the variable auto-mode-alist uses the first match, so I think the pseudo-variable (I don't what is a pseudo-variable; I heard it from phils, seems like a good name) auto-mode-alist in ".dir-locals.el" should also do that, for consistency.

GNU Emacs Manual, 49.2.5 Per-Directory Local Variables:

The special key auto-mode-alist in a .dir-locals.el ... works much like the variable auto-mode-alist (see Choosing File Modes).

GNU Emacs Lisp Reference Manual, 24.2.2 How Emacs Chooses a Major Mode:

If auto-mode-alist has more than one element whose regexp matches the file name, Emacs will use the first match.

However, it seems that the current behavior of dir local auto-mode-alist is to choose a mode from back to front.
The reverse order is so contrary to perception.

  • Please say why you might expect lisp-data-mode. IOW, say what you say in your bug report, vis a vis the behavior difference between auto-mode-alist and entry auto-mode-alist in dir-locals.
    – Drew
    Commented Jul 2, 2023 at 14:15

2 Answers 2


I've reported this as bug#64415; seems like it will be rejected...

To sum up

This bug is about different behavior between the variable auto-mode-alist and the directory variable auto-mode-alist.

The original behavior of how Emacs handles that dir var is to scan the alist from the back to the front, which is very counter-intuitive.

I suggest that we should make that behavior consistent with how Emacs handles the variable auto-mode-alist.

  • Please summarize the bug report.
    – Drew
    Commented Jul 2, 2023 at 14:14
  • 1
    1. Please don't ask questions in answers (except posed to the OP, to clarify the question). 2. I meant to summarize what the bug is, e.g., why you think something is wrong. I didn't mean summarizing why it was closed (but that's OK also, if you think it's helpful).
    – Drew
    Commented Jul 2, 2023 at 14:36
  • What I meant was please describe what the bug is, not just link to it and then summarize opinions about whether the bug should be closed. Otherwise, this is essentially a link-only answer. E.g., "The bug is about different/conflicting behavior between...."
    – Drew
    Commented Jul 2, 2023 at 20:30
  • @Drew: hope I understand what you mean this time
    – shynur
    Commented Jul 3, 2023 at 2:04
  • Yup; you got it. Thx.
    – Drew
    Commented Jul 3, 2023 at 4:30

Edit: This answer applies to Emacs 27.x and earlier. See the note at the end for why.

Firstly, that's not a valid syntax for a dir-locals file -- the top level of the form is not a list of variables/values.

Secondly, what you're attempting is nonsensical -- dir-locals are (can be) per-mode, and so obviously the dir-locals are not checked until the buffer's mode has been set. Changing auto-mode-alist for a buffer after its automatic mode has already been selected is pointless.

Edit: The above applies to Emacs version 27.x and earlier. Emacs 28.1 added support for a new special top-level key auto-mode-alist. Quoting (emacs)Directory Variables in 28.2:

   The special key ‘auto-mode-alist’ in a ‘.dir-locals.el’ lets you set
a file’s major mode.  It works much like the variable ‘auto-mode-alist’
(*note Choosing Modes::).  For example, here is how you can tell Emacs
that ‘.def’ source files in this directory should be in C mode:

     ((auto-mode-alist . (("\\.def\\'" . c-mode))))
  • The basic syntax is ((mode . ((var . value) [(var . value) ...])) [(mode...) (mode...)]) -- so your version is trying to target a major mode symbol auto-mode-alist.
    – phils
    Commented Jul 2, 2023 at 9:38
  • I just followed the example in the emacs manual.
    – shynur
    Commented Jul 2, 2023 at 9:39
  • For which version of Emacs?
    – phils
    Commented Jul 2, 2023 at 9:40
  • 28.2 and 30.0.50
    – shynur
    Commented Jul 2, 2023 at 9:40
  • Ok, I see that Emacs 28 added support for a new pseudo-variable auto-mode-alist in dir-locals forms. My answer applies to older versions of Emacs. I'll add a note...
    – phils
    Commented Jul 2, 2023 at 9:43

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.