Reputation: 3306
Cannot get to bind Enter to newline-and-indent
in Emacs !!! Very annoying.
I already tried everything on the following thread by changing 'mode' to ruby and still nothing:
How do I make Emacs auto-indent my C code?
I know that the problem is the RETURN key, since if I bind to something else, works fine.
I tried [enter]
, (kbd "enter")
, (read-kbd-macro "enter")
, (kbd "RET")
Follow-up 1.
This is what I get from C-hkRET
RET runs the command newline, which is an interactive compiled Lisp function.
It is bound to RET.
(newline &optional ARG)
Insert a newline, and move to left margin of the new line if it's blank. If
use-hard-newlines' is non-nil, the newline is marked with the text-property
hard'. With ARG, insert that many newlines. Callauto-fill-function' if the current column number is greater than the value of
fill-column' and ARG is nil.
I dont know what to make of it or how to figure out if it's a global or local binding that gets in the way. trying to remap C-j also doesnt work.
Upvotes: 2
Views: 3848
Reputation: 73355
As a previous comment says, use C-h k (describe-key) to see what the key is bound to at the point when it's not doing what you want. The (kbd "foo")
syntax will be correct for whichever foo
describe-key refers to it as.
Chances are that you are simply not defining that key in the appropriate keymap.
Note that major and minor mode keymaps take precedence over the global keymap, so you shouldn't necessarily be surprised if a global binding is overridden.
edit:
Myself, I have a hook function for common behaviours for all the programming modes I use, and it includes the sort of remapping you're after. The relevant part looks like this:
(defun my-coding-config ()
(local-set-key (kbd "RET") (key-binding (kbd "M-j")))
(local-set-key (kbd "<S-return>") 'newline)
)
(mapc
(lambda (language-mode-hook)
(add-hook language-mode-hook 'my-coding-config))
'(cperl-mode-hook
css-mode-hook
emacs-lisp-mode-hook
;; etc...
))
See Daimrod's answer for the explanation of why I'm re-binding RET to the current binding of M-j -- although I'm using comment-indent-new-line
(or similar) instead of newline-and-indent
(or similar), which does what I want in both comments and non-comments.
In Emacs 24, programming modes seem to derive from prog-mode
, so you could probably (un-tested) reduce that list to prog-mode-hook
plus any exceptions for third-party modes which don't yet do that.
Upvotes: 6
Reputation: 5030
As said earlier, use C-hkC-j because
C-j is the standard key to do newline-and-indent
.
If you open a new file, activate ruby-mode
and try the previous
command you will see why it doesn't work. Because ruby-mode
doesn't
have newline-and-indent
but rather
reindent-then-newline-and-indent
. Yes that's stupid but you can either ask
to the maintener to change it, or accept it.
However I suggest you to use C-j to do it because
ruby-mode
is not the only mode to do so, like paredit-mode
which
uses paredit-newline
.
Upvotes: 2