Emacs doesn’t automatically indent line for me. For example:
int main() {
int a;
int b; // this line still don't indent with int a
}
So, how to indent line in Emacs ? And moreover, each time I type {, how to make Emacs automatically indent a tab like other IDEs (eclipse, netbean …) ?
Emacs normal behaviour is to reserve the RET (Enter) key for inserting the carriage return literally, while it uses C-j (Ctrl+J) for entering and indenting. This is also so because of a range of other keys that can cause line to be re-indented. By tradition, the commands that cause re-indentation have “electric” as part of their name.
c-modehas a bunch of “electric” commands. To find them, when editing a C source, you can do:C-h b (Ctrl+H B) – list all key bindings in the buffer.
M-s o (Alt PC or Option Mac + S O) – invoke
occurcommand, alternatively, you can M-xoccur.occurprompts you for input (after the previous step, the point will be in minibuffer, so just continue typing).It will open an additional buffer with the content similar to this:
This lists all the commands and the keys assigned to them, which do some “electric” action. You can move point to any of them and by pressing C-h f (Ctrl+H F) RET it will open the help page describing what exactly the command does. For example, if you request help on
c-electric-colonit will show you this:You can continue reading the manual from here by moving the point to the item which looks like a hyperlink (usually it is underlined, or has different visual appearance, then the rest of the text), and hit RET. You can use C-b and C-f in
*Help*mode to navigate back and forward between the pages you’ve already visited (or move the point to the[back]or[forward]buttons and press RET).