Site Overlay

Directory local settings in Emacs

Here’s a neat new trick that I just learned. I’m using the minor mode Projectile to quickly navigate through git repositories. I have one small issue though. The process of generating the list of files in the directory takes quite a while when your repository contains several thousand files.

File caching to the rescue! There’s a small drawback though. Any new file that you create will not show up in the file listing until you’ve visited it.

The slowdown in our large repository is significant, as in it takes several seconds, and I don’t add new files as often as in other repositories. So the trade-off is heavily in favour of caching. I still want caching to be disabled for all other repositories. There must be something Emacs can do about this, right?

Of course there is! Enter .dir_locals.el. It gives us the ability to add local values to variables. The .dir_locals.el file is read and loaded every time you visit a file in the same subtree in the file structure.

If you want to use the .dir_locals.el methor you can just add the following to the file: ((nil . ((projectile-enable-caching . t)))).

I wasn’t comfortable with that solution though. I didn’t want to add a new file to the git repo for a change I wanted in my editor. There’s always the option of adding a global gitignore file, but I didn’t want to that either.

There must be some way to solve this within Emacs? The answer is a resounding YES!, and it’s very easy. Just add the following to your init.el or .emacs.

(dir-locals-set-class-variables 'var_name
   '((nil . ((projectile-enable-caching . t)))))

(dir-locals-set-directory-class
   "/path/to/repository" 'var_name)

For more information see:
* The Emacs Editor – 51.2.5 Per-Directory Local Variables
* Projectile configuration docs
* Projectile on GitHub

Leave a Reply

Your email address will not be published.