16

When in javascript mode trying to use C-c C-m i get an error saying "C-c RET is undefined?"

What makes Emacs believe I am pressing RET?

How can i properly make this keybinding work?

4

2 Answers 2

25

Emacs "thinks" that C-m is RET because "Control M" is the ASCII control character "carriage return". Even though this reason is "historical" Emacs can run in a terminal and so it needs to support the way terminals still work now.

Try opening a terminal window, typing "ls", and pressing C-m. You will see that it is interpreted as "return", even though you are not in Emacs.

See Control character on Wikipedia for details about control characters.

To distinguish C-m from RET in a GUI Emacs, one could change C-i to C-m in @nispio's answer:

(define-key input-decode-map [?\C-m] [C-m])

;; now we can do this:

(defun my-command ()
  (interactive)
  (message "C-m is not the same as RET any more!"))

(global-set-key (kbd "<C-m>") #'my-command)

See also

2
  • 2
    This is brilliant, thank you. Now I can have C-m as a prefix map for multiple-cursors. Commented Dec 9, 2017 at 18:55
  • How come this issue doesn't show up with the other control characters? Commented Jul 8, 2023 at 18:01
0

I have no idea why, but the accepted answer did not work for me. Maybe it is because of how I declare my key bindings.

I use bind-keys like this...

    (bind-keys :map my-mode-map ...)

to bind keys, then I activate my-mode so that my key declarations always have precedence. Using my own mode is new, but I always used bind-keys.

In bind-keys I include the following:

    ([return] . newline) ;; needed to distinguish from C-m

Looking at key declaration help, I do not believe that assigning [return] to newline changes the function of the return key, but declaring [return] causes the Return key to become different than Control-M, allowing me to declare [C-m] later in the same bind-keys command and the two declaratoins are unique.

I use the same technique to separate [tab] from [C-i], first binding [tab], then binding [C-i] in the same bind-keys statement.

Your Answer

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

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