Go to the first, previous, next, last section, table of contents.

# Indentation Commands

Various commands are provided which allow you to conveniently re-indent C constructs, and these are outlined below. There are several things to note about these indentation commands. First, when you change your programming style, either though `c-set-style` or some other means, your file does not automatically get re-indented. When you change style parameters, you will typically need to reformat the line, expression, or buffer to see the effects of your changes.

Second, changing some variables have no effect on existing code, even when you do re-indent. For example, the `c-hanging-*` variables and `c-cleanup-list` only affect newly entered code. So for example, changing `c-hanging-braces-alist` and re-indenting the buffer will not adjust placement of braces already in the file.

Third, re-indenting large portions of code is currently rather inefficient. Improvements have been made since previous releases of `cc-mode`, and much more radical improvements will be made for the next release, but for now you need to be aware of this (8). Some provision has been made to at least inform you as to the progress of your large re-indentation command. The variable `c-progress-interval` controls how often a progress message is displayed. Set this variable to `nil` to inhibit progress messages. Note that this feature only works with Emacs 19.

Also, except as noted below, re-indentation is always driven by the same mechanisms that control on-the-fly indentation of code. See section New Indentation Engine for details.

To indent a single line of code, use `TAB` (`c-indent-command`). The behavior of this command is controlled by the variable `c-tab-always-indent`. When this variable is `t`, `TAB` always just indents the current line. When `nil`, the line is indented only if point is at the left margin, or on or before the first non-whitespace character on the line, otherwise a real tab character is inserted. If this variable's value is something other that `t` or `nil` (e.g. `'other`), then a real tab character is inserted only when point is inside a literal (see section Auto-newline insertion), otherwise the line is indented.

To indent an entire balanced brace or parenthesis expression, use `M-C-q` (`c-indent-exp`). Note that point should be on the opening brace or parenthesis of the expression you want to indent.

Another very convenient keystroke is `C-c C-q` (`c-indent-defun`) when re-indents the entire top-level function or class definition that encompasses point. It leaves point at the same position within the buffer.

To indent any arbitrary region of code, use `M-C-\` (`indent-region`). This is a standard Emacs command, specially tailored for C code in a `cc-mode` buffer. Note that of course, point and mark must delineate the region you want to indent.

While not strictly an indentation function, `M-C-h` (`c-mark-function`) is useful for marking the current top-level function or class definition as the current region.

Go to the first, previous, next, last section, table of contents.