15

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?

Dan
  • 32,980
  • 7
  • 102
  • 169
user3139545
  • 323
  • 3
  • 8

2 Answers2

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

Constantine
  • 9,122
  • 1
  • 35
  • 50
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.

Paul
  • 183
  • 8