Search code examples
emacsindentation

Set 4 Space Indent in Emacs in Text Mode


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. I've added the following to my .emacs:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)

;;; And I have tried
(setq indent-tabs-mode nil)
(setq tab-width 4)

No matter how I change my .emacs file (or my buffer's local variables) the TAB button always does the same thing.

  1. If there is no text above, indent 8 spaces
  2. If there is text on the previous line, indent to the beginning of the second word

As much as I love Emacs this is getting annoying. Is there a way to make Emacs to at least indent 4 space when there's not text in the previous line?


Solution

  • Do not confuse variable tab-width with variable tab-stop-list. The former is used for the display of literal TAB characters. The latter controls what characters are inserted when you press the TAB character in certain modes.

    -- GNU Emacs Manual

    (customize-variable (quote tab-stop-list))
    

    or add tab-stop-list entry to custom-set-variables in .emacs file:

    (custom-set-variables
      ;; custom-set-variables was added by Custom.
      ;; If you edit it by hand, you could mess it up, so be careful.
      ;; Your init file should contain only one such instance.
      ;; If there is more than one, they won't work right.
     '(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120))))
    

    Another way to edit the tab behavior is with with M-x edit-tab-stops.

    See the GNU Emacs Manual on Tab Stops for more information on edit-tab-stops.