Next: , Previous: Major Modes, Up: Top

28 Indentation

This chapter describes the Emacs commands that add, remove, or adjust indentation.

Indent the current line appropriately, in a mode-dependent fashion.
Perform <RET> followed by <TAB> (newline-and-indent).
Merge the previous and the current line (delete-indentation). This would cancel the effect of a preceding C-j.
Split the current line at point; text on the line after point becomes a new line indented to the same column where point is located (split-line).
Move (forward or back) to the first nonblank character on the current line (back-to-indentation).
Indent lines in the region to the same column (indent-region).
C-x <TAB>
Shift lines in the region rigidly right or left (indent-rigidly).
Indent from point to the next prespecified tab stop column (tab-to-tab-stop).
M-x indent-relative
Indent from point to under an indentation point in the previous line.

The <TAB> key runs indent-for-tab-command in most major modes (in C and related modes, <TAB> runs a separate command, c-indent-line-or-region, which behaves similarly). The major mode determines just what this entails.

In text modes, <TAB> inserts some combination of space and tab characters to advance point to the next tab stop (see Tab Stops). If the region is active and spans multiple lines, it advances the first character of each of those lines to the next tab stop (see Using Region). For the purposes of this command, the position of the first non-whitespace character on the preceding line is treated as an additional tab stop. Thus, you can use <TAB> to “align” point with the preceding line.

In programming modes, <TAB> adds or removes some combination of space and tab characters at the start of the line, in a way that makes sense given the text in the preceding lines. If the region is active and spans multiple lines, all those lines are indented this way. If point was initially within the current line's indentation, it is positioned after that indentation; otherwise, it remains at same point in the newly-indented text. See Program Indent.

Normally, indentation commands insert (or remove) an optimal mix of tab characters and spaces to align to the desired column. Tab characters (ASCII code 9) are displayed as a stretch of empty space extending to the next display tab stop. By default, there is one display tab stop every eight columns; the number of columns is determined by the variable tab-width. You can insert a single tab character by typing C-q <TAB>. See Text Display.

The command M-i (tab-to-tab-stop) adjusts the whitespace characters around point, inserting just enough whitespace to advance point up to the next tab stop. By default, this involves deleting the existing whitespace and inserting a single tab character.

See Just Spaces, for how to disable use of tabs. However, C-q <TAB> always inserts a tab, even when tabs are disabled for the indentation commands.

The variable tab-always-indent tweaks the behavior of the <TAB> (indent-for-tab-command) command. The default value, t, gives the behavior described above. If you change the value to the symbol complete, then <TAB> first tries to indent the current line, and if the line was already indented, it tries to complete the text at point (see Symbol Completion). If the value is nil, then <TAB> indents the current line only if point is at the left margin or in the line's indentation; otherwise, it inserts a real tab character.