4

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?

4 Answers 4

4

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.

2
  • (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, 2009 at 3:07
  • Perhaps. Are you also setting a style or a mode hook?
    – bk1e
    Aug 18, 2009 at 16:20
2
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.
2

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)
1

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .