Search code examples
emacselispcc-mode

Change Emacs C style not working


For Emacs CC-mode, I am trying to use the "bsd" style, but make it so that all lines default to indentation in increments of 4 instead of 8. In my .emacs file, I have put:

(setq c-default-style "bsd"
      c-basic-offset 4)
(setq c-indent-level 4)

But all lines still indent to 8 spaces. I can't really locate where the problem is. I'm running GNU Emacs 23.3.1.


Solution

  • CC-mode settings are buffer-local which can cause problems. The best way to configure it is to put your customizations in a hook. That will ensure that, regardless of whether cc-mode has made c-basic-offset buffer local or not, the changes will be applied when the mode is started. I use something similar to this:

    (defun my-c-mode-hook ()
      (setq c-basic-offset 4
            c-indent-level 4
            c-default-style "bsd"))
    (add-hook 'c-mode-common-hook 'my-c-mode-hook)