8

I am able to bind M + RET with:

(define-key my-keys-mode-map (kbd "<M-RET>") 'my-function)

How can I bind C + RET with the above format?

I have tried:

  • kbd "<C-RET>"
  • kbd "C-<RET>"
  • kbd "C-RET"

But none of these work.

If it is impossible with define-key and kbd, is there another way to bind C + RET in the mode my-keys-mode-map?

0

3 Answers 3

13

You need not guess how Emacs represents the key sequence that corresponds to your pressing and holding the Ctrl key while pressing the Enter or Return key.

You can instead ask Emacs itself to tell you. Just do C-h k followed by using those keys. In my Emacs, with my keyboard, Emacs tells me this:

<C-return> runs the command ...

So I know that what I need to pass to kbd is the string "<C-return>".

So, as @glucas says, I can use this (and you probably can too - but ask Emacs just how it sees those keys):

(define-key my-keys-mode-map (kbd "<C-return>") 'my-function)
4
  • 1
    As I said in my comment on glucas's answer, my emacs only seems to see <RET> when I hit C-return. Do you have any ideas why that is happening?
    – Ben
    Mar 10, 2017 at 18:26
  • You don't cite the actual text of the help output that you say indicates that Emacs sees only <RET>. Anyway, perhaps keys Ctrl + Return with your keyboard sends only what Return sends. Do other apps besides Emacs distinguish the key combination Ctrl + Return for you?
    – Drew
    Mar 10, 2017 at 18:57
  • Emacs' output on C-h k C-return: "RET runs the command newline, which is an interactive compiled Lisp function in simple.el. It is bound to RET." How do you recommend testing whether apps besides Emacs see C-return for me?
    – Ben
    Mar 10, 2017 at 19:20
  • 2
    It sounds like your keyboard sends just ^M (Control-M, aka RET) for C-return. If you are running Emacs in terminal mode that might be the case. Maybe you have another application where you can distinguish C-return from RET? Or maybe not.
    – Drew
    Mar 10, 2017 at 22:02
1

Does this work:

(define-key my-keys-mode-map (kbd "<C-return>") 'my-function)

More generally, try using describe-key to see what Emacs "sees" for a given key combination. C-h followed by your key sequence (control + return) gives me:

<C-return> is undefined

So I can use "<C-return>" as the argument for kbd.

1
  • 1
    When I execute M-x describe-key C-return, is says it is bound to just RET. I can obviously use other control combinations, but emacs doesn't seem to recognize control combined with return. I am on Mac OS, using emacs 24.3.1 via ssh.
    – Ben
    Mar 10, 2017 at 16:06
1

According to docs

RET -> for terminal mode

<return> -> for GUI mode

So, you might be confusing the two.

3
  • What docs? It can be either key for either terminal or GUI, depending on the terminal. And this doesn't respond to a question about the key being modified with modifier key Ctrl.
    – Drew
    Nov 4, 2017 at 23:46
  • could not find actual doc again but take a look at this ergoemacs.org/emacs/emacs_key_notation_return_vs_RET.html was the doc on my Ubuntu: (eval-after-load 'ensime '(define-key ensime-mode-map (kbd "<C-return>") 'ensime-inf-eval-region)) works fine. Nov 5, 2017 at 15:10
  • The doc you link to is simply wrong. You can both keys or either key in either a terminal or a GUI session, depending on the terminal you use.
    – Drew
    Nov 5, 2017 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.