1

This is probably a really basic question, but something I hadn't thought about before: When editing code, how to indent? I think the way to go is simply use the tab key and then, once indented once, Emacs can work out that you are on an indented line, and when you press return, you get back to the same indentation level. And if you tab once more, you are at the second indentation level.

On my Emacs the indentation levels seem to be two characters each. In my .emacs file, I see '(tab-width 2) — presumably the reason I get two-character indents?

If I go to whitespace-mode, I see there are no tab characters even though I did press tab for the indent. In my .emacs file, I see (setq-default indent-tabs-mode nil).

Does indent-tabs-mode being nil change tabs to spaces?

1 Answer 1

1

C-h f indent-tabs-mode tells you:

Indentation can insert tabs if this is non-`nil`.

So if you want TAB chars then leave the default value as t.


Does indent-tabs-mode nil change tabs to spaces?

No, indent-tabs-mode does not change existing TAB chars to SPC chars. To remove TAB chars, changing each to one or more SPC chars, use command untabify. That works on the active region or, with a prefix arg, the whole buffer.

2
  • What about my first question, I used to type spaces, eg 2 spaces to indent, but emacs seems to allow me to enter a tab character, which is quicker and then it converts to spaces (as per your answer). Presumably the more typing efficient way to indent is to use the tab key?
    – arcomber
    Commented May 28 at 7:59
  • 1
    No the answer shows you that if you set these then you will insert tabs. The tab-width setting is telling emacs how to display the tab
    – mmmmmm
    Commented May 28 at 15:58

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.