3

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

(use-package google-c-style
  ;; provides the Google C/C++ coding style
  :ensure t
  :config
  (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.

space_voyager
  • 709
  • 5
  • 19

1 Answers1

5

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!

ebpa
  • 7,319
  • 26
  • 53
  • I don't understand the approach here - where do I put these 3 lines such that a .dir-locals.el is generated? – space_voyager Dec 21 '16 at 09: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)` – Resigned June 2023 Sep 06 '17 at 04:21