So I'm trying to setup default indentation in Emacs. Currently I have the following at the bottom of my .emacs file as per a number of tutorials I've found on the internet.

(setq-default indent-tabs-mode nil)
(setq tab-width 3)
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-ident-level 'tab-width)
(defvaralias 'js-indent-level 'tab-width)

Except... it doesn't work; when I open a C++ code file, tab-width appears to be set to 8. Can someone tell me why this is, and also what I need to do to fix it?

  • Check the docstring. <f1> v tab-width RET
    – npostavs
    Sep 22 '16 at 19:10
  • I did, it wasn't helpful Sep 22 '16 at 19:20
  • Automatically becomes buffer-local when set. should be your clue
    – npostavs
    Sep 22 '16 at 19:45
  • Yes I read that, but I don't really understand how that causes this problem. Assume I'm a relative newbie Sep 22 '16 at 20:05
  • Ok, never mind, I get it. Sep 22 '16 at 20:07

Ok, turns out the answer to my question was the following:

tab-width automatically becomes buffer local when set. The buffer local version derives from the default value, so I needed to use setq-default not setq

  • Note that you could also set the tab-width by mode in the mode hook, in case you want different tab widths across different languages.
    – Dan
    Sep 22 '16 at 22:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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