Directory-Local Variables

Just in case there's still anyone out there who isn't following Artur Malabarba, he has a nice post on something that I had almost forgotten about. It turns out that you can set Emacs variables on a per directory basis. These directory-local variables are especially handy for setting project variables by adding them to the root directory of your project.

The process is easy and Emacs will guide you through it if you call add-dir-local-variable. You can even arrange to have a bit of Elisp code executed so the facility is reasonably general. Take a look at Malabarba's post for the details. If you're working on projects where you'd like to have a special configuration, this could be a real help.

This entry was posted in General and tagged . Bookmark the permalink.
  • Phil

    It's a reminder of just how much functionality Emacs provides when dedicated long-term users can be nearly oblivious to things that other users think of as essential functionality :) I make such heavy use of directory-local variables that this post made me do a bit of a double-take. Then I thought about all the many many things I'm only vaguely aware of in Emacs that will undoubtedly be essential functionality to others, and realised I'm in exactly the same boat!

    • jcs

      Yup. As you say, that's the power of Emacs. Everyone can get it set up to use exactly the stuff they need for a (nearly) frictionless workflow and still only use part of the power. That's why I love reading and writing about Emacs. I always learn something new and sometimes it's something useful to me and it sticks.

  • Phil

    The approach that I suspect fewer people are aware of is to use the dir-locals-set-class-variables and dir-locals-set-directory-class functions to configure your dir-locals in your init file, without using a .dir-locals.el file at all. I mostly use this approach, and it is especially useful when there are certain predictable paths which will occur anywhere you install Emacs. An example is setting (tab-width . 8) for all Emacs sources.