I would like to set the major mode for a file as you would using file-local variables

Local Variables:
mode: text

but in .dir-locals.el in order not to “pollute” the file in question. Is this possible?

  • Is this purely for personal use, or something you want to be shareable? There are several ways you can tackle this in elisp, rather than resorting to directory-local variables. – phils Nov 14 '15 at 5:12
  • @phils It would be nice to have a shareable solution, which is why I thought about dir-locals. But practically, it is more or less personal. (Unlikely to be shared with many people, and who knows if they use emacs and/or care). – xebtl Nov 15 '15 at 20:20
  • Ok. I think utilising the eval pseudo-variable is the only way to handle it in a .dir-locals.el file. For custom elisp, you might just look at using auto-mode-alist to match the filename regexp (you can use regexp-quote to create patterns for verbatim string values, if necessary). – phils Nov 15 '15 at 20:52

It's possible to specify a string instead of a mode, but this only works for subdirectories. So I'll use nil instead to match all modes, then the eval key to change the major mode conditionally:

   (lambda ()
     (when (string= (file-name-nondirectory buffer-file-name)

A downside of this approach is that the eval key is unsafe, so you'll need to confirm the variable permanently for this to have an effect.

| improve this answer | |
  • Thanks, this works. Could someone explain the difference with the following form? ``` ((nil . ((eval . (lambda () (when (string= (file-name-nondirectory (buffer-name)) "filename") (my-mode))))))) ``` – Swarnendu Biswas May 15 at 4:32

The following .dir-locals.el works just fine for me to set the major mode of all files in a directory to shell-script-mode:

((nil . ((mode . shell-script))))

The obvious downside is that you can't specify the major mode for only a given subset of files in the directory.

Addendum: Amusingly, it also has the side effect of setting the major mode of .dir-locals.el itself to shell-script-mode.

Addendum 2: Rather more amusingly, it also has the side effect of setting the major mode of Dired buffers to shell-script-mode, making it impossible to open any (as an error is thrown during the initialization).

| improve this answer | |

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.