I'm using company-mode for completion in C++ code. To tell clang backend of company where the include files for the current project are I have to use the following .dir-locals.el file

((c++-mode (eval setq company-clang-arguments (append 

I have to specify the full path always. Is there a way to replace full/path/to/project/root with a variable that stores the path the the .dir-locals.el file?

I tried

 (c++-mode (eval setq company-clang-arguments (append
                                               (list concat "-I" default-directory "src")))))

but since default-directory is evaluated in the file that I'm opening it doesn't return to path the the project root but some other path inside the project.


I found the solution with projectile. It has a function projectile-project-root which can be used to get the project path.

I can noe use the following in .dir-locals.el and it will still work when I move the project or use it on another machine.

((nil . ((eval . (progn
                   (require 'projectile)
                   (setq company-clang-arguments (delete-dups (append
                                                  (list (concat "-I" (projectile-project-root) "src"))))))))))

Only downside is that it you need projectile and that the project has to be a projectile project (not much of a problem since every folder with git/bzr/hg/... is a valid project).


As pointed out in the comments it is also possible to use

(locate-dominating-file default-directory ".dir-locals.el")

instead of projectile-project-root.

  • 3
    (locate-dominating-file default-directory ".dir-locals.el") may also be an option, maybe even abbreviated into a function.
    – politza
    Jun 7 '15 at 20:30
  • This is pretty much what projectile-project-root does. Thanks for the tip though.
    – kain88
    Jun 8 '15 at 7:29
  • @politza: I don't think that works: it will pick up the closest .dir-locals.el, not the one that the form comes from.
    – Clément
    Jul 20 '16 at 2:32
  • I think that using dir-locals-file variable instead of ".dir-locals.el" string is more appropriate
    – sivakov512
    Jan 15 '17 at 5:37

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.