Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bind command to C-RET in Emacs

Tags:

emacs

Say I have some interactive function in Emacs my-function, how can I bind it to Ctrl + RET?

I have tried with:

(global-set-key (kbd "C-RET") 'my-function)

and

(global-set-key (kbd "C-return") 'my-function)

but none of them seem to work. is this at all possible?

like image 353
Amelio Vazquez-Reina Avatar asked Apr 07 '13 23:04

Amelio Vazquez-Reina


People also ask

How do you bind commands in Emacs?

Keys can be bound to commands either interactively or in your . emacs file. To interactively bind keys for all modes, type M-x global-set-key RET key cmd RET . To bind a key just in the current major mode, type M-x local-set-key RET key cmd RET .

What is RET key in Emacs?

"RET" is the Return key while emacs runs in a terminal. and run emacs in terminal, your keybinding will have no effect. But the problem is, by binding (kbd "RET") , you are also binding (kbd "C-m") , regardless you run emacs in terminal or GUI.

What does C mean in Emacs?

C- means "control key", Hold down the control key while. pressing the next key. M- means "meta key", (“Escape” in the lab, on other computers sometimes “Alt”).


1 Answers

Always remember that kbd very conveniently accepts the exact same syntax that Emacs gives you when you ask it about a key sequence, so you never ever have to guess.

C-hkC-RET tells me:

<C-return>

therefore I would use (kbd "<C-return>")

OTOH, when running Emacs in my terminal, C-hkC-RET tells me:

C-j

because C-RET isn't a valid control character in a terminal, and therefore Emacs isn't receiving the same input that it gets in GUI mode (so I wouldn't be able to use that binding in my terminal).

like image 144
phils Avatar answered Sep 21 '22 01:09

phils