Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 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.

share|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
add comment

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.

share|improve this answer
add comment
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.
share|improve this answer
add comment

You should try (setq-default tab-width 4)

I have the following settings in my .emacs file, and it works for me:

(setq-default tab-width 2)
(setq js-indent-level 2)
(setq c-basic-offset 4)
(setq css-indent-offset 2)
(setq sh-basic-offset 2)
(setq-default indent-tabs-mode nil)
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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