Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn off alternative Enter with Ctrl+M in Linux

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.

like image 248
Alex Avatar asked Feb 19 '10 18:02

Alex


People also ask

What is Ctrl-M in Linux?

Viewing the certificate files in Linux shows ^M characters appended to every line. The file in question was created in Windows and then copied over to Linux. ^M is the keyboard equivalent to \r or CTRL-v + CTRL-m in vim.

How do I get rid of M in vim?

How can I remove ^M characters from text files? A. ^M are nothing more than carriage return, so it is easy to use the search and replace function of vim to remove them. That will do the job.

How do you type Ctrl-M character in Unix?

Note: Remember how to type control M characters in UNIX, just hold the control key and then press v and m to get the control-m character.


2 Answers

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.

like image 88
Eric Warmenhoven Avatar answered Oct 26 '22 23:10

Eric Warmenhoven


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.

like image 25
Trey Jackson Avatar answered Oct 27 '22 01:10

Trey Jackson