emacs: indenting text and code
When new programming languages appear, a major mode for Emacs that does basic syntax highlighting and indenta- tion appears almost immediately. But i think auto format is more important, no matter what indent you write, code always tobe same format, that means you don't need care about indent at all.
TAB: Indenting the Current Line
| Key and Command | Description |
|---|---|
| TAB | Indents line using major mode's indentation command |
| M-i | Inserts spaces or tabs to next tab stop |
| M-x edit-tab-stops | Edits tab stops |
Disabling tab characters
If you dislike the use of tab characters and if you prefer whitespace, customize the variable indent-tabs-mode.
Changing the amount of indentation
The variable tab-width controls how many characters of spacing each tab uses. It also controls the amount of whitespace to use if you disabled indent-tabs-mode.
Indenting Regions
| Key and Command | Description |
|---|---|
| TAB | Indents a line or region as per the major mode |
| C-M-\ | Indents using major mode’s region indent command |
| C-x TAB | Rigidly indents |
In an ideal world, pressing TAB with an active region is all you need to re-indent it. Unfortunately, Emacs might not support that, or in some programming languages it is not physically possible to determine the correct indentation. Pressing TAB follows most of the same rules as line indentation: Emacs attempts to indent according to the indent-line-function and it falls back on simply inserting TAB characters (or whitespace, if you disabled indent-tabs-mode).
Typing C-M- explicitly indents the region; for some modes it works identically to TAB and in others it doesn’t. If you give the command a numeric argument, it will indent the region to that column (i.e., the number of characters) and Emacs will also use your fill prefix (if you have one) and fill the text accordingly. C-M- is occasionally useful as it respects your fill prefix. However, if you want to indent a fixed number of columns, you should use C-x TAB.
C-x TAB explicitly indents the region a certain number of columns. It
also takes negative and numeric arguments. However, if you don’t pass an
argument, Emacs will en- ter an arrow-key-driven indentation mode that
lets you interactively indent the region with
S-<left> and S-<right>.