I often find myself in the situation where I'm told to put relative paths as directory local variable. For example cmake-ide's cmake-ide-project-dir and cmake-ide-build-dir. Of course this is not very portable.

So instead of


((nil . ((cmake-ide-project-dir . "/home/user/code/project"))))

I want something like

((nil . ((cmake-ide-project-dir . directory-of-current-dir-locals-file))))

How can I define such a variable directory-of-current-dir-locals-file? And how would I set for example cmake-ide-build-dir, which is typically (concat directory-of-current-dir-locals-file "build")?


My solution so far (based on this Stackoverflow answer):


;;; Directory Local Variables
;;; For more information see (info "(emacs) Directory Variables")

((nil . ((eval . (set (make-local-variable 'my-project-path)
                       (let ((d (dir-locals-find-file ".")))
                         (if (stringp d) d (car d))))))
         (cmake-ide-project-dir . my-project-path)
         (eval . (setq cmake-ide-build-dir (concat my-project-path "build")))
  • 5
    You can use also (locate-dominating-file default-directory ".dir-local.el")
    – ibizaman
    Oct 26 '17 at 5:24
  • 1
    @ibizaman It should be ".dir-locals.el": (locate-dominating-file default-directory ".dir-locals.el") Apr 5 at 19:36

You could define variable with (defvar directory-of-current-dir-locals-file "/home/user/code/project").

  • 2
    Sorry maybe my question was not clear. But the idea was to not use absolute paths. Since you know you want to set the variable to something relative to the .dir-locals.el file.
    – Flow
    Aug 5 '16 at 13:23

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.