I have used Vim for years now, Emacs for a couple of weeks and recently switched to Spacemacs. How do I set indentation settings either globally or the C/C++ mode? (A solution for either one is fine.) I've put the following in my dotspacemacs/user-config () function in my .spacemacs file.

  (setq-default indent-tabs-mode t)
  (setq-default tab-width 4)                ; tabs four spaces wide
  (defvaralias 'c-basic-offset 'tab-width)  ; Set the C/C++/Java.. mode to use this tab width

But neither is it using tabs (it's using spaces) nor is the tab-width 4 (it's 2). Is this supposed to work? If not, what's the way to set tab indentation and tab-width for either a specific mode or globally in Spacemacs? (The settings above worked for me in the init.el for vanilla Emacs.)

  • Don't use tabs. It makes your code display differently across different editors and operating systems.
    – Qudit
    May 9, 2018 at 3:09
  • Well.. that's kind of the point. I want readers of my code to be able to adjust it to their preferences. However, I do want to align code (with respect to the line above) with spaces after the indentation level. I just want the indentation level to be attained with tabs. Eg., some people find 8-length indentation easier to read. I want 4-length indentation. But I'll think about it - perhaps using spaces will make my life easier. May 10, 2018 at 4:02
  • It's a good idea, but for instance your configuration will not display code that assumes a tab width of 8 correctly. Just try viewing some of the Emacs source code and you will see what I mean.
    – Qudit
    May 10, 2018 at 6:07

1 Answer 1


Emacs doesn't blindly add shifts when pressing TAB. Instead, it uses "indentation style" to deduce indentation intelligently from the surrounding mode.

For example with the default indentation style, gnu, Emacs uses 2 spaces.

To change the indentation mode type C-c . when editing the file, and choose preferred mode (use TAB for completion). For example I mostly use stroustrup, which uses 4 spaces.

To make it permanent, set the c-default-style variable in your init file:

(setq c-default-style "stroustrup")

More about C indentation: EmacsWiki - Indenting C

  • Oh I didn't know about styles. Thanks. However, even with the Stroustrup style, it still uses spaces. I added (setq-default indent-tabs-mode t) (setq-default tab-width 4) back, and I think it's good now. May 10, 2018 at 3:57
  • But those options did simply work in Emacs and I could combine them with smart-tabs-mode. Spacemacs seems more picky. May 10, 2018 at 4:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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