I'm trying to activate flycheck-mode by default when opening any file in my project's directory. I'm a bit of an elisp newbie, my .dir-locals.el file looks like this:

((nil . (mode . flycheck-mode))
 ((cmake-ide-dir . "~/workspace/Samira3D"))
 ((cmake-ide-build-dir . "~/workspace/Samira3D/build/")))

And the error I'm getting is

Invalid function: (nil mode . flycheck-mode)

How would I go about solving this?

  • 1
    You don't call functions in .dir-locals.el. The file only contains a list that encodes how the variables are set. See the manual for details. The reason that you are getting this error I think is that flycheck mistreats the file as a usual el file.
    – xuhdev
    Commented Apr 30, 2016 at 9:01
  • 1
    I'd try whether the eval keyword is supported like with file-local variables.
    – wasamasa
    Commented Apr 30, 2016 at 9:24
  • I tried: ((nil . (cmake-ide-build-dir . "~/workspace/Samira3D/build"))) and it still gives me "Invalid function: (nil cmake-ide-build-dir . "~/workspace/Samira3D/build")" @xuhdev Commented Apr 30, 2016 at 9:48
  • 1
    @xuhdev, of course, you can call functions in .dir-locals.el
    – RichieHH
    Commented Feb 23, 2020 at 11:52

1 Answer 1

  1. Your list nesting is wrong.
  2. You need to use the eval pseudo-variable to evaluate expressions / call functions.
  3. Using mode to set a minor mode is deprecated. That's now for major modes only, and minor modes are enabled via eval.

I think you wanted this:

((nil . ((eval . (flycheck-mode 1))
         (cmake-ide-dir . "~/workspace/Samira3D")
         (cmake-ide-build-dir . "~/workspace/Samira3D/build/"))))

Also have a read of this:

C-hig (elisp) Dotted Pair Notation RET

Sometimes you'll see examples of .dir-locals.el files using fewer dots and parentheses, and the difference (or indeed equivalence) between the different ways of formatting them might be confusing, so that will hopefully clarify the matter.

  • 1
    Nowadays you have to update safe-local-eval-forms for eval to work if enable-local-variables is :safe.
    – gavenkoa
    Commented Jan 29, 2021 at 21:08

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.