1

I understand from here, that I can use dir-locals.el to customize modes, such as given in the example:

      ;; Warn about spaces used for indentation:
      (haskell-mode . ((eval . (highlight-regexp "^ *"))))
      (c-mode . ((c-file-style . "BSD")))
      (java-mode . ((c-file-style . "BSD")))
      ("src/imported"
       . ((nil . ((change-log-default-name . "ChangeLog.local"))))))

However, all of these make 1 change per language-mode. I would like to make multiple changes to the same mode, python-mode, and I think my syntax is only keeping the first one.

What's the right syntax to combine these two variables settings in dir-locals.el?

I have:

;; misc
((python-mode
  (python-shell-buffer-name . "Python[This Project]")))

;; set the local pyvenv
((python-mode . ((pyvenv-activate . "./envs/default")
                 )))

;; find local pylintrc vs global
((python-mode . ((eval . (lambda ()  (setq flycheck-pylintrc ".pylintrc"))))))

Other refs consulted:

3
  • 2
    For each mode you can have an alist. The alist can specify any number of settings. See the example with nil (which stands for any mode) in the Emacs Wiki page you cite. Change nil to python-mode and you have an example of setting multiple things for Python mode. Commented Nov 10, 2020 at 18:46
  • Could you point me to an example? Commented Dec 23, 2020 at 21:22
  • It's like that TV game show where the contestants are given an answer and have to respond with a matching question. Commented Jun 24, 2022 at 7:34

2 Answers 2

5

This is already answered, but just for another perspective:

If you just want to avoid syntax problems or view correct syntax, you can call add-dir-local-variable. It prompts for a mode, then a variable, then a setting. You can do this twice for the one mode, then open your .dir-locals.el file and view the correct syntax.

1
  • n.b. This works best since Emacs 27.1, when the command was changed so that it generated the preferred dotted-pair notation. Commented Jun 24, 2022 at 11:40
4

The dir-locals file contains an alist of nested alists. This would be the correct form for your example of setting multiple variables for python-mode:

((python-mode . ((python-shell-buffer-name . "Python[This Project]")
                 (pyvenv-activate . "./envs/default")
                 (eval . (lambda ()  (setq flycheck-pylintrc ".pylintrc")))))
 (other-mode . (... ALIST)))

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.