Next: , Previous: Major Modes, Up: Top


28 Indentation

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

<TAB>
Indent the current line appropriately, in a mode-dependent fashion.
C-j
Perform <RET> followed by <TAB> (newline-and-indent).
M-^
Merge the previous and the current line (delete-indentation). This would cancel the effect of a preceding C-j.
C-M-o
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).
M-m
Move (forward or back) to the first nonblank character on the current line (back-to-indentation).
C-M-\
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).
M-i
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.

In most major modes, the <TAB> key runs the command indent-for-tab-command, which either performs indentation or inserts whitespace at point, depending on the situation.

In programming modes such as Lisp mode and C mode, <TAB> indents the current line if the region is inactive. If the region is active, it indents every line in the region (see Mark). Indentation means adding or removing some combination of space and tab characters (whitespace characters) at the start of the line, in a way that makes sense given the text in the preceding lines. Exactly how indentation is performed depends on the major mode. See Program Indent.

In text modes, <TAB> inserts some whitespace characters to advance point to the next tab stop (see Tab Stops). 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. You can therefore use <TAB> to “align” point with the preceding line. If the region is active, <TAB> performs this action on every line in the region.

Indentation is often performed with the help of tab characters (ASCII code 9), which 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.

Normally, most of these indentation commands insert an optimal mix of tabs and spaces to align to the desired column. 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.