I am working on a cmake project and I would like to set a directory variable containing the correct compile command. I added this to the root directory of the project in a .dir-locals.el file:

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

((nil .
      ((indent-tabs-mode . t)
       (fill-column . 80)
       (compile-command . "cd build && make"))))

If I enter describe-variable for the variable fill-column, I get the message that This variable's value is directory-local, set by the file .... The variable is set from its default of 70 to 80 instead. However, the compile-command variable is unchanged. Is there a bug in the file?

3 Answers 3


The variable compile-command is not buffer local by default, thus you can not set it per buffer. To change this add the following to your init file:

(make-variable-buffer-local 'compile-command)

I believe the compilation command and its history in the majority of cases should be directory local rather than buffer local.

To accomplish this, I've been planning for quite some time to modify the behavior of compilation-read-command to automatically read the value of compile-command and compile-history if present in the file .dir-locals.el under default-directory. Has anybody else tried this?


At least with Emacs 29 compile-command seems to be buffer-local, even the documentation gives:

# Local Variables:
# compile-command: "cc foo.c -Dfoo=bar -Dhack=whatever \
#   -Dmumble=blaah"
# End:

as working, which I confirm.

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.