There are some default key bindings in Emacs, that is what you get when you have brand new Emacs installation.

The first thing you will do may be start to customizing key bindings. For some of the built in configuration its easy, but there are some bindings that pretty hard to rebind it and get everything work.

In this post we talk about Ctrl-m and Enter.

You know there are two unprintable character in ASCII table can represent to the concept of new line: the line feed and Carriage Return, the decimal and hexadecimal are 10, 13 and 0a 0d.

In Emacs's convention, Enter is denotated as "<return>" and Ctrl-m is "RET".And the Ctrl-m generate Carriage Return . It has many different representation form, the 13 and 0d in ASCII, or ^M for text representation, or \r for escaping.

And the Enter key which is "<return>" in Emacs convention will be translated to "RET".Even in modern convention this key is the standard key to new line or Carriage Return, but in Emacs its nothing special, the standard Carriage Return in Emacs is Ctrl-m, Enter key is just translated to it, itself has nothing to do the concept of new line or Carriage Return.

What is this mean? When you input command in mini buffer and press Enter to execute it or write expression in IELM(the ELISP interpreter) and press Enter to execute it. This is OK because the Enter is translated to "REM". In all those environments, "RET" or ^M means Carriage Return.

This behavior is hard code into Emacs's LISP code base.

For example, look at the ielm.el.

 
 
(defvar ielm-map
  (let ((map (make-sparse-keymap)))
    (define-key map "\t" 'comint-dynamic-complete)
    (define-key map "\C-m" 'ielm-return)
    (define-key map "\C-j" 'ielm-send-input)
    (define-key map "\e\C-x" 'eval-defun)         ; for consistency with
    (define-key map "\e\t" 'completion-at-point)  ; lisp-interaction-mode
    ;; These bindings are from `lisp-mode-shared-map' -- can you inherit
    ;; from more than one keymap??
    (define-key map "\e\C-q" 'indent-sexp)
    (define-key map "\177" 'backward-delete-char-untabify)
    ;; Some convenience bindings for setting the working buffer
    (define-key map "\C-c\C-b" 'ielm-change-working-buffer)
    (define-key map "\C-c\C-f" 'ielm-display-working-buffer)
    (define-key map "\C-c\C-v" 'ielm-print-working-buffer)
    map)
  "Keymap for IELM mode.")
 

The "\C-m" is bound to ielm-return. You can find more in other modes. For example the minibuffer

 
(let ((map minibuffer-local-map))
  (define-key map "\C-g" 'abort-recursive-edit)
  (define-key map "\r" 'exit-minibuffer)
  (define-key map "\n" 'exit-minibuffer))
 

Here both line feed and Carriage Return are bound to exit-minibuffer. The \n is line feed ,in Emacs its Ctrl-j.

This convention actually is rare in most modern editors. Because of this, you should be careful when rebind Ctrl-m.

First, when you rebind C-m, you should also rebind Enter . For example, I want to rebind C-m to delete char backward:

 
(global-set-key "\C-m" 'delete-backward-char)
 

You should rebind Enter like these

 
 
(global-set-key (kbd "<return>") 'newline)
 
 

Because Enter by default will be translated to "RET", so change key binding of C-m which is equivalent to "RET" in Emacs will also rebind the Enter to the same command. We must rebind Enter to prevent it being translated.

Now then Enter will execute newline, and C-m delete char backward. But Enter in minibuffer or IELM won't work anymore. As I said above, C-m is hard coded in those places. Now Enter is not translated into "RET", but merely execute newline command.

But Enter is much nature in those places. What we need to do is rebind in all those placed.

For IELM

 
 
(add-hook 'ielm-mode-hook
  (lambda ()
     (define-key ielm-map (kbd "<return>") 'ielm-return)))
 
 

For minibuffer

 
 
(define-key minibuffer-local-map (kbd "<return>") 'exit-minibuffer)
 
 

In Help mode and button

 
 
(eval-after-load 'help-mode
  '(define-key help-mode-map (kbd "<return>") 'help-follow-symbol)
)
 
(define-key button-map (kbd "<return>") 'push-button)