I recently upgraded to Emacs24, and a number of my custom keybindings broke as a result of it.
According to the fine manual, it is possible to make Emacs stop conflating function keys with their ASCII control codes (eg, it is possible to have C-m
and RET
bound to different things, or C-i
and TAB
, and so on). This has always been a large pet peeve of mine with Emacs, that such valuable "first level" keyboard shortcuts are wasted on things for which I already have dedicated keys on my keyboard. I want to bind them to different things, in my case, to 'modernize' the keybindings by mimicking gedit. In Emacs23, this was working beautifully:
(global-set-key (kbd "C-i") 'goto-line)
(global-set-key (kbd "C-m") 'comment-or-uncomment-region)
(global-set-key (kbd "C-d") 'kill-whole-line)
;; Fix some stuff broken by the above
(global-set-key [delete] 'delete-char)
(global-set-key (kbd "TAB") 'indent-for-tab-command)
(global-set-key (kbd "RET") 'newline)
Then, I upgraded to Emacs24 and it broke, kinda. It still "works" in the sense that C-m
certainly does one thing, and RET
does another, but the problem is that the return key no longer behaves properly in terminal mode or in the minibuffer. Instead of activating the command I've just typed, in both cases, the return key simply moves the cursor down to the next line and I'm left with no way of activating the commands I type in to either the minibuffer or the terminal.
Ironically, Emacs24 introduced a lot of changes to the behavior of deleting, and in the process they decoupled C-d
from DEL
so that it's actually now safe to bind C-d
to something without needing to bind DEL
back to the expected behavior, so it would be great if I could achieve similar "it just works" behavior for my return key, while C-m
is bound to something else.
So, I can envision two possible solutions to this problem. One might look like this:
(global-set-key (kbd "C-m") 'comment-or-uncomment-region)
(global-set-key (kbd "RET") 'do-what-i-expect-the-return-key-to-do-in-any-mode)
OR, something like this would be even nicer:
(setq decouple-ascii-control-codes-from-function-keys t)
But I'm not aware of any such variable or function that would help me out in this scenario.
I've made several unsuccessful attempts at using mode-hooks to restore the correct bindings in terminal and minibuffer modes, but I just can't seem to get anything to work. Help!
Thanks.
This seems to work:
(add-hook 'find-file-hook
(lambda ()
(local-set-key (kbd "C-m") 'comment-or-uncomment-region)
(local-set-key (kbd "<return>") 'newline-and-indent)))
The idea here is that instead of tinkering with the return key globally (which is what breaks the terminal and minibuffer buffers), we only set these keybindings on a per-buffer basis, except that we do it unconditionally for all buffers that represent files on disk.
It's a little bit inefficient, having to run every time I open a file, but it's nice insofar as I don't have to think of every possible mode to "fix", it simply doesn't break terminal/minibuffer/etc modes in the first place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With