ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

30.3.1 Basic Program Indentation Commands

The basic indentation commands indent a single line according to the usual conventions of the language you are editing.

<TAB>
Adjust indentation of current line.
C-j
Insert a newline, then adjust indentation of following line (newline-and-indent).

The basic indentation command is <TAB>. In any programming-language major mode, <TAB> gives the current line the correct indentation as determined from the previous lines. It does this by inserting or deleting whitespace at the beginning of the current line. If point was inside the whitespace at the beginning of the line, <TAB> puts it at the end of that whitespace; otherwise, <TAB> keeps point fixed with respect to the characters around it. If the region is active (see Mark), <TAB> indents every line within the region instead of just the current line. The function that <TAB> runs depends on the major mode; for instance, it is c-indent-line-or-region in C mode. Each function is aware of the syntax and conventions for its particular language.

Use C-q <TAB> to insert a tab character at point.

When entering lines of new code, use C-j (newline-and-indent), which inserts a newline and then adjusts indentation after it. (It also deletes any trailing whitespace which remains before the new newline.) For instance, C-j at the end of a line creates a blank line with appropriate indentation. In programming language modes, it is equivalent to <RET> <TAB>.

When Emacs indents a line that starts within a parenthetical grouping, it usually places the start of the line under the preceding line within the group, or under the text after the parenthesis. If you manually give one of these lines a nonstandard indentation, the lines below will tend to follow it. This behavior is convenient in cases where you have overridden the standard result of <TAB> indentation (⁖, for aesthetic purposes).

Many programming-language modes assume that an open-parenthesis, open-brace or other opening delimiter at the left margin is the start of a function. This assumption speeds up indentation commands. If the text you are editing contains opening delimiters in column zero that aren't the beginning of a functions—even if these delimiters occur inside strings or comments—then you must set open-paren-in-column-0-is-defun-start. See Left Margin Paren.

Normally, Emacs indents lines using an “optimal” mix of tab and space characters. If you want Emacs to use spaces only, set indent-tabs-mode (see Just Spaces).

blog comments powered by Disqus