In switching coding styles, I need to go from a one tab width of 4 indentation to a two space indentation.

I've added the following to my .emacs file, at the very end:

(setq indent-tabs-mode nil)
(setq c-basic-offset 2)

Unfortunately, that's not working. I still need to M-x set-variable for c-basic-offset, setting it to 2 for each file I open, to successfully change the indentation to 2 spaces instead of 1 tab.

Is there another config setting I need to add after these settings to make the new c-basic-offset stick?

link|improve this question
feedback

3 Answers

c-basic-offset is a per-buffer variable, so setting it in your .emacs won't work. You can use (setq-default c-basic-offset 2) or you can define a mode hook that executes every time you start cc-mode.

link|improve this answer
(setq-default c-basic-offset 2) at the end of my .emacs file doesn't have an effect. Could another variable be overwriting the desired behaviour? – kitt Aug 18 '09 at 3:07
Perhaps. Are you also setting a style or a mode hook? – bk1e Aug 18 '09 at 16:20
feedback

Possible alternate approach: use something like (set-variable 'c-file-style "gnu") to get the style you want. There is a pretty comprehensive list of built in styles, and you can define your own.

link|improve this answer
feedback
M-x customize-variable c-basic-offset

From the docstring:

This variable automatically becomes buffer-local when set outside Custom.
However, setting it through Custom sets the default value.
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.