0

I have the following line in my .emacs file:

(setq-default indent-tabs-mode nil)

but sometimes my indents still come through as tabs rather than spaces in c-mode. It is inconsistent; roughly 70% of my c-mode indents are made up of spaces and the rest of them are tabs. I haven't seen this issue in any other major mode.

Here is the only c-mode hook I have specified in my .emacs file:

(add-hook 'c-mode-hook (lambda () (setq comment-start "//"
                                        comment-end "")))

I'm not certain, but I think the tabs rather than spaces might happen only when auto-indenting. Does anyone have any ideas why this might be the case?

I'm on Emacs 29.1, but also see the behavior on Emacas 28.3 (I haven't tried other versions).

1
  • Check that indent-tabs-mode really has value nil in the buffers where you see creation of indentation with TAB chars. It's Automatically becomes buffer-local when set. And it can be a file-local variable.
    – Drew
    Nov 20, 2023 at 20:37

1 Answer 1

0

Firstly, you may have messed up the indentation while using an old version of your config that did not set indent-tabs-mode properly. Try running C-x h TAB to fix the indentation of the whole buffer and see if tabs appear again.

Secondly, make sure your c-default-style does not set the value of indent-tabs-mode, for example the "linux" one sets it to t.

Finally, you can create your own C style and use it by either setting it as the default for c-default-style or by calling c-set-style in the c-mode-hook.

If you want to make it simpler (I do not claim that it will work though), you can set the indentation style in each buffer individually, for example by augmenting your hook :

(defun my-c-setup ()
  "Custom setup for C."
  (interactive)
  (setq-local comment-start "//"
              comment-end ""
              indent-tabs-mode nil))

(add-hook 'c-mode-hook 'my-c-setup)
1
  • Thanks! The issue was the c-default-style
    – tedtanner
    Nov 24, 2023 at 19:00

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.