dbmikus
dbmikus

Reputation: 5339

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.

Upvotes: 1

Views: 2335

Answers (2)

ataylor
ataylor

Reputation: 66109

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)

Upvotes: 9

Vanessa MacDougal
Vanessa MacDougal

Reputation: 992

That looks right. Have you executed those lines in your .emacs file? Go to the end of them and do C-x C-e. If that doesn't work, close emacs and restart it (which forces it to re-read your .emacs file). If that doesn't work, go to your scratch buffer or something similar, and execute (message "%s %d %d" c-default-style c-basic-offset c-indent-level) to see what emacs thinks those values are (maybe you are including another file that overwrites those values?) Then load one of your CC files and check the mode to be sure you are in a C-type mode.

Upvotes: 2

Related Questions