5

I use (setq-default indent-tabs-mode nil) in my init file so to avoid using TABs but when I write some files (like C++ files) then it allows me to insert TABs if I am in an if-statement for example. Why is that happening?

Also I thought initially that (setq-default indent-tabs-mode nil) prevents the usage of TABs completely but I have seen in a lot of answers that it converts TABs to spaces. How is it doing that?

5

Major modes and minor modes are allowed to locally change the indent-tabs-mode variable, and a lot of them do. That's why (setq-default indent-tabs-mode nil) might not always be enough.

This is what I use.

(defun endless/c-hook ()
  (setq indent-tabs-mode nil))
(add-hook 'c++-mode-hook #'endless/c-hook)
4

Not sure what you are asking. Non-nil indent-tabs-mode means that Emacs can use TAB chars instead of a number of consecutive SPC chars. If the value is nil in a given buffer then indentation by Emacs (e.g. automatic indentation) uses only SPC chars. But even in that case there is nothing preventing you from inserting a TAB char (e.g., using C-q TAB).

What setq-default does determines only the default behavior. In any given buffer (e.g., in any given mode), the value can be different from the default (aka global) value. It is the buffer-local value that determines the behavior (as described in the previous paragraph).

"Conversion" of TAB chars to one or more consecutive SPC chars happens if indent-tabs-mode is nil and you hit DEL (the Backspace key) or you change the code in such a way that the appropriate indentation column changes. All that happens is that if a TAB char needs to be removed to meet the new indentation level then SPC chars replace it as needed. Existing TAB chars that do not need to be removed to get proper indentation are not removed.


(On a different but related subject, be aware that you can replace all existing TAB chars in a buffer by doing C-x h M-x untabify.)

  • What I am asking is why am I allowed to use TABS in some files when I have (setq-default indent-tabs-mode nil) in my init file. Not by pressing C-q TAB but just TAB. Also does that by default converts TABs to spaces? – Adam May 6 '15 at 4:53
  • 2
    Adam: Check the actual value for any given buffer with C-h v indent-tabs-mode -- that will at least tell you whether it has the value you thought it had. As Drew points out, any mode (or other elisp) is free to override the default value that you've set. – phils May 6 '15 at 6:19
  • 2
    Also note that indent-tabs-mode doesn't govern whether or not the buffer may contain tabs, and changing the value has no immediate automatic effect on the buffer contents. The value only affects what happens to the affected line(s) when Emacs performs an indentation action. – phils May 6 '15 at 6:28
  • See also C-h v backward-delete-char-untabify regarding the conversion of tabs to spaces when deleting indentation. – phils Nov 7 '18 at 1:25
1

This is in addition to Malabarba's answer.

I had this same issue. I realized that the indent-tabs-mode value was being persisted in each buffer.

If you have the problem and can't resolve it, make sure to regenerate your buffers (kill buffer and re-open files). I am new to emacs, so there probably is a better solution, but this worked for me.

  • This is more of a comment than an answer. – Drew Mar 8 '17 at 1:19
  • True, but you must have a higher reputation in order to comment on other's answers. This was the only alternative I could think of. – tomcat Mar 9 '17 at 1:59
0

(setq indent-tabs-mode nil) in c++-mode-hook

  • 4
    Please consider extending your answer a bit. As it stands, it does not fully address OPs question(s). – itsjeyd May 7 '15 at 12:45

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.