Dellowar
Dellowar

Reputation: 3352

indent-tabs-mode is still using spaces

Below is my init.el script. It is loading because the column-indicator is visible. However editing any C file, the tab key still produces 4 spaces when I want only tabs (\t). I have never used lisp and I'm new to emacs, what's wrong?

(setq-default indent-tabs-mode t)
(require 'fill-column-indicator)


(defun my-c-mode-hook ()
  (setq-default c-default-style "bsd"
                c-basic-offset 4
                tab-width 4
                indent-tabs-mode t)

  (c-set-offset 'substatement-open 0)
  (fci-mode))

(add-hook 'c-mode-hook 'my-c-mode-hook)

Upvotes: 2

Views: 1202

Answers (1)

phils
phils

Reputation: 73246

Try this:

(setq c-default-style "bsd")

(defun my-c-mode-hook ()
  (setq c-basic-offset 4
        tab-width 4
        indent-tabs-mode t)

  (c-set-offset 'substatement-open 0)
  (fci-mode))

(add-hook 'c-mode-hook 'my-c-mode-hook)

Variables have a global value and, potentially, a buffer-local value.

Furthermore certain variables will automatically use a buffer-local value when you set them. Such variables include c-basic-offset, tab-width, and indent-tabs-mode (as you can see for yourself if you describe them via C-hv)

setq-default sets the global value of a variable, but by the time that c-mode-hook runs the buffer-local values have already been established, so setting the default value at that point isn't really what you want, as it doesn't affect the existing local values (although depending on how the mode works, doing so may cause future buffers to have the desired values).

setq sets the buffer-local value when one exists (and the global value otherwise), so this is what you want to use for most of those variables.

c-default-style conversely is not automatically buffer-local, so there should be no purpose in setting that with c-mode-hook. We can just set its (global) value once.

Upvotes: 3

Related Questions