Set 4 Space Indent in Emacs in Text Mode

EmacsIndentation

Emacs Problem Overview


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?

Emacs Solutions


Solution 1 - Emacs

Short answer:

The key point is to tell emacs to insert whatever you want when indenting, this is done by changing the indent-line-function. It is easier to change it to insert a tab and then change tabs into 4 spaces than change it to insert 4 spaces. The following configuration will solve your problem:

(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)

Explanation:

From Indentation Controlled by Major Mode @ emacs manual:

> An important function of each major > mode is to customize the key to > indent properly for the language being > edited. > > [...] > > The indent-line-function variable is > the function to be used by (and > various commands, like when calling > indent-region) to indent the current > line. The command > indent-according-to-mode does no more > than call this function. > > [...] > > The default value is indent-relative > for many modes.

From indent-relative @ emacs manual:

> Indent-relative Space out to under next > indent point in previous nonblank line. > > [...] > > If the previous nonblank line has no > indent points beyond the column point > starts at, `tab-to-tab-stop' is done > instead.

Just change the value of indent-line-function to the insert-tab function and configure tab insertion as 4 spaces.

Solution 2 - Emacs

Update: Since Emacs 24.4:

> tab-stop-list is now implicitly extended to infinity. Its default value is changed to nil which means a tab stop every tab-width columns.

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))

Solution 3 - Emacs

> 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.

Solution 4 - Emacs

You may find it easier to set up your tabs as follows:

M-x customize-group

At the Customize group: prompt enter indent.

You'll see a screen where you can set all you indenting options and set them for the current session or save them for all future sessions.

If you do it this way you'll want to set up a customisations file.

Solution 5 - Emacs

(setq tab-width 4)
(setq tab-stop-list '(4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80))
(setq indent-tabs-mode nil)

Solution 6 - Emacs

(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)

Solution 7 - Emacs

(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

Solution 8 - Emacs

This problem isn't caused by missing tab stops; it's that emacs has a (new?) tab method called indent-relative that seems designed to line up tabular data. The TAB key is mapped to the method indent-for-tab-command, which calls whatever method the variable indent-line-function is set to, which is indent-relative method for text mode. I havn't figured out a good way to override the indent-line-function variable (text mode hook isn't working, so maybe it is getting reset after the mode-hooks run?) but one simple way to get rid of this behavior is to just chuck the intent-for-tab-command method by setting TAB to the simpler tab-to-tab-stop method:

(define-key text-mode-map (kbd "TAB") 'tab-to-tab-stop)

Solution 9 - Emacs

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.

Solution 10 - Emacs

Try this:

(add-hook 'text-mode-hook
  (function
   (lambda ()
     (setq tab-width 4)
     (define-key text-mode-map "\C-i" 'self-insert-command)
     )))

That will make TAB always insert a literal TAB character with tab stops every 4 characters (but only in Text mode). If that's not what you're asking for, please describe the behavior you'd like to see.

Solution 11 - Emacs

Just changing the style with c-set-style was enough for me.

Solution 12 - Emacs

Add this to your .emacs file:

This will set the width that a tab is displayed to 2 characters (change the number 2 to whatever you want)

(setq default-tab-width 2)

To make sure that emacs is actually using tabs instead of spaces:

(global-set-key (kbd "TAB") 'self-insert-command)

As an aside, the default for emacs when backspacing over a tab is to convert it to spaces and then delete a space. This can be annoying. If you want it to just delete the tab, you can do this:

(setq c-backspace-function 'backward-delete-char)

Enjoy!

Solution 13 - Emacs

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)

Solution 14 - Emacs

This is the only solution that keeps a tab from ever getting inserted for me, without a sequence or conversion of tabs to spaces. Both of those seemed adequate, but wasteful:

(setq-default
    indent-tabs-mode nil
    tab-width 4
    tab-stop-list (quote (4 8))
)

Note that quote needs two numbers to work (but not more!).

Also, in most major modes (Python for instance), indentation is automatic in Emacs. If you need to indent outside of the auto indent, use:

M-i

Solution 15 - Emacs

The best answers did not work for until I wrote this in the .emacs file:

(global-set-key (kbd "TAB") 'self-insert-command)

Solution 16 - Emacs

Have you tried

(setq  tab-width  4)

Solution 17 - Emacs

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

Solution 18 - Emacs

By the way, for C-mode, I add (setq-default c-basic-offset 4) to .emacs. See http://www.emacswiki.org/emacs/IndentingC for details.

Solution 19 - Emacs

From my init file, different because I wanted spaces instead of tabs:

(add-hook 'sql-mode-hook (lambda () (progn (setq-default tab-width 4) (setq indent-tabs-mode nil) (setq indent-line-function 'tab-to-tab-stop) (modify-syntax-entry ?_ "w") ; now '_' is not considered a word-delimiter (modify-syntax-entry ?- "w") ; now '-' is not considered a word-delimiter )))

Solution 20 - Emacs

Modified this answer without any hook:

(setq-default
  indent-tabs-mode t
  tab-stop-list (number-sequence 4 200 4)
  tab-width 4
  indent-line-function 'insert-tab)

Solution 21 - Emacs

To make in text-mode pressing Tab does indent then tabbing/spacing by fixed values (NOT by previous line words)

see also: indent-relative-first-indent-point, tab-width indent-tabs-mode

(add-hook 'text-mode-hook
 (lambda()
   (progn
     (setq tab-always-indent nil) 
     ;(setq electric-indent-mode nil)
     (setq indent-line-function 
           (lambda()
             (indent-relative 't)   
             )
           ) 
     (setq tab-always-indent nil)
     )))

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCristianView Question on Stackoverflow
Solution 1 - EmacsalcortesView Answer on Stackoverflow
Solution 2 - EmacsphilsView Answer on Stackoverflow
Solution 3 - EmacsBert FView Answer on Stackoverflow
Solution 4 - EmacsDave WebbView Answer on Stackoverflow
Solution 5 - EmacsYan LiView Answer on Stackoverflow
Solution 6 - EmacslawlistView Answer on Stackoverflow
Solution 7 - Emacsuser2318996View Answer on Stackoverflow
Solution 8 - EmacsGlennView Answer on Stackoverflow
Solution 9 - EmacsgigilibalaView Answer on Stackoverflow
Solution 10 - EmacscjmView Answer on Stackoverflow
Solution 11 - EmacsdividebyzeroView Answer on Stackoverflow
Solution 12 - Emacsqwerty9967View Answer on Stackoverflow
Solution 13 - EmacsYaryView Answer on Stackoverflow
Solution 14 - EmacsryanpcmcquenView Answer on Stackoverflow
Solution 15 - Emacsuser1009285View Answer on Stackoverflow
Solution 16 - EmacsGrokCodeView Answer on Stackoverflow
Solution 17 - EmacswaseemqView Answer on Stackoverflow
Solution 18 - EmacsflyrainView Answer on Stackoverflow
Solution 19 - EmacsforkandwaitView Answer on Stackoverflow
Solution 20 - EmacsSaurabhView Answer on Stackoverflow
Solution 21 - Emacsalex_1948511View Answer on Stackoverflow