My .cxx files have 8-character indentation, while .cpp files have 4-character one. They have exact same modes enabled:

cxx vs cpp

My auto-mode-alist has the following c++ lines:

("\\.ii\\'" . c++-mode) ("\\.h\\'" . c-or-c++-mode) ("\\.c\\'" . c-mode) ("\\.\\(CC?\\|HH?\\)\\'" . c++-mode) ("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode) ("\\.\\(cc\\|hh\\)\\'" . c++-mode)

How can I fix the .cxx files?

  • Are your .cxx files indented with tabs?
    – nega
    Mar 21 '20 at 18:48
  • @nega within the buffer I move through indentation as spaces. I also tried setting tab-width to 4 and that did not change anything Mar 21 '20 at 19:14
  • The cc-modes (c++-mode, c-mode, java-mode, etc) won't auto-re-indent your files. So if there's already 8 spaces when you load it, that's what Emacs will show you.
    – nega
    Mar 21 '20 at 19:23
  • @nega Excellent! I ran M-x + indent-region (C-M-\ ), saved and now it opens properly. Also, when I copy new code into new .cxx file it opens with 4-character indentation. Could you please turn your comment into an answer for me to accept it? Thank you for your help! Mar 22 '20 at 12:45

As was pointed out by nega in the comments, I should have re-indented the file after fixing offset in my init.el file:

(setq c-default-style "linux"
      c-basic-offset 4); <-- this changes the default value 8 to 4.

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.