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,584
  • 6
  • 98
  • 168
user3139545
  • 323
  • 3
  • 8
  • 2
    Possible duplicate of [Properly distinguish Ctrl+i and Tab](http://emacs.stackexchange.com/questions/17509/properly-distinguish-ctrli-and-tab) – Drew Feb 12 '16 at 13:35
  • 1
    Same problem, different entry point, I think it would be nice to keep both questions around for people coming from different searches. – Jordon Biondo Feb 12 '16 at 15:58
  • 1
    [Meta question about whether this question is a duplicate](http://meta.emacs.stackexchange.com/questions/368/same-problem-different-entry-point-is-it-a-duplicate) – Gilles 'SO- stop being evil' Feb 14 '16 at 00:38
  • 1
    Possible duplicate of [How to bind C-i as different from TAB?](http://emacs.stackexchange.com/questions/220/how-to-bind-c-i-as-different-from-tab) – Gilles 'SO- stop being evil' Feb 14 '16 at 00:39

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,072
  • 1
  • 34
  • 49
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