views:

327

answers:

4

Why is Ctrl+M bound to Enter in Ubuntu Jaunty? How to turn it off?

I'm using Emacs and would like to bind Ctrl+M to some other command.

A: 

(global-set-key (kbd "C-m") 'cmd) ;

Where cmd is your command should remap control m...

As for why ctrl+m is bound to enter. I believe it had something to do with some older keyboard not having enter,tab, backspace, etc... ( I could be grossly mistaken)

For example ctrl+h is backspace, some unix operating systems will output ^H when you hit backspace on them!

Sumason
It works, but it also translates RET to cmd.
Alex
Okay, what I wanted to say is that since Enter is interpreted as <return> and it in its turn is translated to RET, Enter starts to work as cmd as well as Ctrl+m.
Alex
+12  A: 

I think your question is backwards. It is not C-m that is bound to Enter, it is Enter that is bound to C-m. And C-m is the same as RET.

If you run C-h k C-m, you will see something like "RET runs the command ...". C-m sends RET because it is a control code, see http://en.wikipedia.org/wiki/Control_character.

The Enter key is bound to C-m; if you run C-h k Enter, you will see something like "RET (translated from <return>) runs the command ...". See, Enter is being interpreted by emacs as <return> and then that key is getting translated to C-m.

What you want to do is first remove the translation from <return> to RET by binding it directly to what it's currently indirectly bound, e.g. (global-set-key (kbd "<return>") 'newline). Then you're free to bind C-m to whatever you want without affecting Enter.

This assumes you're using the graphical emacs. If you're running it in a terminal, this won't work, because Enter will send C-m, not <return>. You can check that using the window-system variable though.

Eric Warmenhoven
@Eric: Where can I see "RET runs the command ..."?
Alex
'C-h k' runs the command 'describe-key', which will ask you which key you want to describe, then pop up a help buffer which tells you what command the key is bound to and what that command does.
Eric Warmenhoven
+3  A: 

Note: The issue isn't limited to Linux, it exists on Windows (and presumably Mac) as well. Read the other (non stack-overflow) source of all knowledge: Wikipedia on Carriage Return.

If you want to rebind C-m, be sure to all bind <return> otherwise you run the risk of no longer being able to use the Enter/Return key. Also, in a terminal, Emacs cannot distinguish between the two (C-m and <return>).

In a plain Emacs, the Enter/Return key is bound to <return>, which is (by default) translated to RET (same thing as C-m). If you only rebound the C-m, you'd also be affecting the Enter/Return key.

Try C-h k <return> and you'll see

RET (translated from <return>)

So, rebind both in the appropriate keymap to make sure you get the behavior you want.

It might be instructive to play with the following code:

(defun my-return ()
  (interactive)
  (message "return"))
(defun my-ret ()
  (interactive)
  (message "RET"))
(defun my-c-m ()
  (interactive)
  (message "C-m"))
(global-set-key (kbd "<return>") 'my-return)
(global-set-key (kbd "C-m") 'my-c-m)
(global-set-key (kbd "RET") 'my-ret)

Put that in your *scratch* buffer and press C-j after each line (to evaluate the sexp). Then play with the Enter/Return keys and C-m.

Trey Jackson
+2  A: 

The main source of the problem is that Enter and Ctrl-M both map to the same ASCII code (13). You would only be able to map them distinctly on a system that can distinguish them.

Gabe