Set 4 Space Indent in Emacs in Text Mode

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.

Leave a Comment