Next: , Previous: Major Modes, Up: Top


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

Emacs supports four general categories of operations that could all be called `indentation':

  1. Insert a tab character. You can type C-q <TAB> to do this.

    A tab character is displayed as a stretch of whitespace which extends to the next display tab stop position, and the default width of a tab stop is eight. See Display Custom, for more details.

  2. Insert whitespace up to the next tab stop. You can set tab stops at your choice of column positions, then type M-i to advance to the next tab stop. The default tab stop settings have a tab stop every eight columns, which means by default M-i inserts a tab character. To set the tab stops, use M-x edit-tab-stops.
  3. Align a line with the previous line. More precisely, the command M-x indent-relative indents the current line under the beginning of some word in the previous line. In Fundamental mode and in Text mode, <TAB> runs the command indent-relative.
  4. The most sophisticated method is syntax-driven indentation. Most programming languages have an indentation convention. For Lisp code, lines are indented according to their nesting in parentheses. C code uses the same general idea, but many details are different.

    Type <TAB> to do syntax-driven indentation, in a mode that supports it. It realigns the current line according with the syntax of the preceding lines. No matter where in the line you are when you type <TAB>, it aligns the line as a whole.

Normally, most of the above methods 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.