0

In Emacs' C++ mode, I sometimes accidentally introduced a tab where I should use four whitespaces instead to create indentations. Adjacent lines with mixed uses of tabs and spaces for indentations are shown to be aligned in C++ mode.

But when I open the C++ file thus created in another editor, for example in Eclipse CDT editor, it will show that the indentations of the lines mentioned above are not actually aligned, because of mixed uses of tabs and spaces.

This makes me realized that Emacs C++ mode is not actually a pure text editor. A tab is probably not shown as wide as four whitespaces, and that makes me add more or less tabs than I should for aligning with other lines' indentations.

In Emacs C++ mode, how can I avoid making such mistakes?

Thanks.

  • 1
    (setq-default indent-tabs-mode nil) prevents using TAB for indenting. But it does not remove existing TAB chars already in a file. For that you need to use C-x h M-x untabify. – Drew Aug 29 '16 at 23:07
  • 2
    Possible duplicate of Confusion by the usage of indent-tabs-mode nil – Drew Aug 29 '16 at 23:09
3

The normal convention is that TAB characters align to the next multiple-of-8 column. Notice: I wrote 8, not 4.

So if your other text editor aligns to a multiple-of-4 column (like you seem to imply), it could be argued that it's a bug in that other editor (tho it's more likely that you have customized that editor (explicitly/knowingly or not) to use a "tab-width" of 4).

As mentioned by Drew, a good way to deal with these problems is to try and avoid inserting TAB characters, which you typically do by setting indent-tabs-mode to nil.

If you really thing TABs chars should indent to a multiple-of-4 column, then you can also try and customize all the tools you ever use to look at those files correspondingly. In the case of Emacs, you'd do that with (setq tab-width 4). I'd recommend against this practice, tho.

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.