Problem: When a C++ file is loaded, the indentation rules are not always what they should be, i.e. instead of indenting by 3, it will indent by 1 or 8 for that session. This occurs consistently through the buffer, as if I have changed the variable for offset and/or indent.

Here is the code I have in general configurations file that is loaded by my init.el:

;;No tabs, set to 3 spaces when found
(setq-default indent-tabs-mode nil)
(setq-default tab-width 3)

;;C AND C++
(setq c-default-style "linux"
   c-basic-offset 3
   c-basic-indent 3
   indent-tabs-mode nil)

To reiterate: sometimes the file will load and the indentation rules, as given above, will load correctly and work just fine. However, very often it will not and will have a 'new' rule in the session, indenting by some new amount X of spaces. Going to M-x Customize Variable and checking c-basic-offset and c-basic-indent shows they are custom set and c-default-style shows linux as it should.

The only other thing I have to say is that emacs does not know it has loaded a C++ file all the time and will say C/l mode instead of C++/l mode, requiring me to manually switch to that. Regardless, when indentation isn't working as it should, both modes act up. When it isn't, both modes indent as they should. Other modes that I have loaded (that I could see /potentially/ influencing this) include Company(vanilla), Flycheck, and Electric Indent.

My emacs configuration isn't crazy or that complex, so I have no problem giving my github here if that will help.

  • You will be more likely to get a useful answer if your provide some steps to reproduce the behavior you are talking about from emacs -q.
    – nispio
    Oct 18, 2016 at 0:01
  • I cannot find any reference to c-basic-indent anywhere in my version of GNU Emacs, GNU Emacs 25.2.2 (Ubuntu 18.04).
    – bgoodr
    Aug 25, 2018 at 14:30
  • this question is nearly two years old and was made, iirc, with emacs 24.3. It has long since been removed as a variable for configuration. For basic use cases, c-basic-offset should be used instead for configuration.
    – zaile
    Sep 11, 2018 at 2:32

1 Answer 1


Okay, since I added the following line immediately after the block of code given above, I haven't had any problems:

(setq c-guess-current-offset nil)

Whether this was an actual solution or not is not perfectly clear, but for anyone else you might run into this, it's worth a try.

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.