Is it possible to load .dir-locals.el file without prompting me, but also without adding values I am configuring in that file to my init.el in custom-set-variables? The reason I want it is that that file contains some sensitive data and I have a habit to share my init.el, so I want it to just load. I understand that in general .dir-locals.el might contain inappropriate code that I should not evaluate. So I was thinking that probably there is something like whitelist of .dir-locals.el.


In general what I want to achieve is to have 'project'-wide emacs configurations file that is loaded without any additional hassle. So if it's not possible with .dir-locals.el - I am open to other solutions :D

3 Answers 3


I added this to my init.el:

(defun recursive-load-dir-settings (currentfile)
  (let ((lds-dir (locate-dominating-file currentfile "settings.el")))
  (when lds-dir
        (load-file (concat lds-dir "settings.el"))
        (recursive-load-dir-settings (file-truename(concat lds-dir "..")))))))

(defun load-dir-settings()
  (when buffer-file-name
    (recursive-load-dir-settings buffer-file-name)))

(add-hook 'find-file-hook 'load-dir-settings)

This loads the file settings.el in the current file's directory. It then continues searching for settings.el upwards in the directory tree, until no more such files can be found (just like .dir-locals.el).

This loads and runs the settings files without a security prompt, so you need to be sure that you are the only one who can put such files in the directory tree. If you still want the prompt, it could of course be added without using the customize mechanic, so your init.el could stay clean.

An example for settings.el might look like this:

(make-local-variable 'my-pw)
(setq my-pw "passw0rd A")
(message "Loaded super-secret password for project A")
  • This looks like an answer I've been looking for. Thank you, I'll give it a try.
    – d12frosted
    Commented Aug 16, 2015 at 10:40

If you write configuration to your init.el file manually, then you could add the following:

(setq custom-file "~/.emacs.d/custom.el")
(load custom-file t)

Now when emacs encounters a .dir-locals.el with iffy options Emacs will save your choice into this custom.el file, leaving you free to share your init.el file publicly.

If you like to use Emacs' Customize interface for all your configuration, I don't think this will work for you.

  • Thanks for your answer. This workaround might be good for me. But unfortunately, it's not general solution, so I am leaving question open.
    – d12frosted
    Commented Aug 15, 2015 at 18:01

Solution is to override Emacs dir-locals protection system.

(defun safe-local-variable-p (sym val)
"Put your guard logic here, return t when sym is ok, nil otherwise"

Simply put it somewhere in your init and monkey-patch emacs elisp core.

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.