I have in my ~/.emacs.d/init.el:

(use-package google-c-style
  ;; provides the Google C/C++ coding style
  :ensure t
  (add-hook 'c-mode-common-hook 'google-set-c-style))

However, a particular C project I'm viewing uses

(setq c-default-style "linux"
      c-basic-offset 4)

How can I override the default google C style with this one for this specific project? Something along the lines of putting something inside of .dir-locals.el placed at the root of the project would be nice.


A .dir-locals.el file would certainly do the trick!

I like to use the function (add-dir-local-variable MODE VARIABLE VALUE) rather editing .dir-locals.el manually. For those two values eval

(let ((default-directory "~/your/project/path"))
     (add-dir-local-variable nil 'c-default-style "linux")
     (add-dir-local-variable nil 'c-basic-offset 4))

and save the created .dir-locals.el file!

  • I don't understand the approach here - where do I put these 3 lines such that a .dir-locals.el is generated? Dec 21 '16 at 9:40
  • 1
    @space_voyager One way to eval the expression is by putting the cursor after the last closing parentheses and press C-x C-e. You can do this from nearly any buffer (you could paste the code into scratch for example). Once you've run the code above you don't need it anymore since the code creates the .dir-locals.el file. Be sure to save the new .dir-locals.el file that emacs opens when it is run.
    – ebpa
    Dec 21 '16 at 15:43
  • (put 'c-default-style 'safe-local-variable #'stringp) Sep 6 '17 at 4:21

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.