Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set 4 Space Indent in Emacs in Text Mode

People also ask

How do I indent multiple lines in Emacs?

Select multiply lines, then type C-u 8 C-x Tab , it will indent the region by 8 spaces.

How many spaces is a tab in Emacs?

This is because standard tabs are set to eight spaces. Tabs are special characters.

How do I replace tabs with spaces in Emacs?

To replace tabs with the appropriate number of spaces, use M-x untabify . To do the reverse and convert multiple spaces to tabs, you can use M-x tabify . Both commands work on a region. To run on the whole buffer use a prefix argument (i.e. C-u M-x untabify ).


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.


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

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.


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.