I\'ve been unsuccessful in getting Emacs to switch from 8 space tabs to 4 space tabs when pressing the TAB in buffers with the major mode text-mode.
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(defun my-custom-settings-fn ()
(setq indent-tabs-mode t)
(setq tab-stop-list (number-sequence 2 200 2))
(setq tab-width 2)
(setq indent-line-function 'insert-tab))
(add-hook 'text-mode-hook 'my-custom-settings-fn)
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
(setq c-default-style "linux")
(setq c-basic-offset 4)
(c-set-offset 'comment-intro 0)
this works for C++ code and the comment inside too
Customizations can shadow (setq tab width 4) so either use setq-default or let Customize know what you're doing. I also had issues similar to the OP and fixed it with this alone, did not need to adjust tab-stop-list or any insert functions:
(custom-set-variables
'(tab-width 4 't)
)
Found it useful to add this immediately after (a tip from emacsWiki):
(defvaralias 'c-basic-offset 'tab-width)
(defvaralias 'cperl-indent-level 'tab-width)
You can add these lines of code to your .emacs file. It adds a hook for text mode to use insert-tab instead of indent-relative.
(custom-set-variables
'(indent-line-function 'insert-tab)
'(indent-tabs-mode t)
'(tab-width 4))
(add-hook 'text-mode-hook
(lambda() (setq indent-line-function 'insert-tab)))
I hope it helps.
Update: Since Emacs 24.4:
tab-stop-listis now implicitly extended to infinity. Its default value is changed tonilwhich means a tab stop everytab-widthcolumns.
which means that there's no longer any need to be setting tab-stop-list in the way shown below, as you can keep it set to nil.
Original answer follows...
It always pains me slightly seeing things like (setq tab-stop-list 4 8 12 ................) when the number-sequence function is sitting there waiting to be used.
(setq tab-stop-list (number-sequence 4 200 4))
or
(defun my-generate-tab-stops (&optional width max)
"Return a sequence suitable for `tab-stop-list'."
(let* ((max-column (or max 200))
(tab-width (or width tab-width))
(count (/ max-column tab-width)))
(number-sequence tab-width (* tab-width count) tab-width)))
(setq tab-width 4)
(setq tab-stop-list (my-generate-tab-stops))