I have come to hate the inconsistency of the electric indent mode and I haven't much of a clue about how it works anyways, so I've disabled it and am trying to get the desired behavior from scratch. I want emacs to indent me to the previous line's indentation level and do nothing else on RET
. indent-relative
seems to be the right function for the job. Seems easy enough, and yet I cannot get it to work. This is what I have thus far:
(electric-indent-mode -1)
(defun my-newline-and-indent ()
(interactive)
(newline)
(indent-relative))
I used to bind this with global-set-key
, but hooking it to the specific modes I use is a better idea (found here):
(defun my-keys ()
(local-set-key [(return)] 'my-newline-and-indent))
(add-hook 'c-mode-hook 'my-keys)
The above does not indent new lines in C. I also tried the same thing with indent-relative-maybe
, but that doesn't work either. What am I doing wrong?
Edit: now that I got it working thanks to @Tyler, the function that better fits the behavior I want is actually indent-relative-maybe
(though I can't find any documentation on it online). indent-relative
indents to the last whitespace on the previous line, which is too zealous. For example, here is how it indents in F90 mode:
program test
_
By contrast, indent-relative-maybe
seems to only indent to the first non-whitespace character.
newline-and-indent
do what you want?int main() {
, press enter, and the cursor moves to the next line under them
in main. Pressing two slashes and return, the cursor moves to the next line under the first slash.c-mode-hook
in your init file.