7

The key "RET" is bound to 'newline in many modes in Emacs as described in M-x describe-binding. But in the minubuffer, they are different (try to send the command newline to the minibuffer). So this question arises: In the minibuffer, to what function the physical "RET" key is bound.

Name
  • 7,689
  • 4
  • 38
  • 84

2 Answers2

14

You are confusing a few things, here.

First, RET is not a physical (keyboard) key. It is a logical key -- the Emacs way of writing what your physical Return or Enter key typically sends to Emacs: a Control-M character (which Emacs also writes as C-m, when describing the Ctrl + m key sequence). And if your physical Return or Enter key sends something else, then Emacs writes that as the pseudo-function key <return>. And Emacs automatically maps <return> to RET, unless <return> is itself bound to a different command than RET. (Note that the ASCII control character Control-M is also called a carriage return character, and C-m is essentially another way of writing RET.)

Second, the minibuffer uses its own keymaps. And in those RET is generally bound to a different command than it is outside the minibuffer. Similarly, C-j is bound to a different command that it is outside the minibuffer.

Control J is the ASCII control character for a newline -- the newline character. So in some Emacs modes C-j inserts a newline character (sometimes written \n). This is a convention -- nothing forces a correspondence between the key sequence C-j and insertion of a newline (i.e., Control J) character.

But C-j does not do this in the minibuffer, not by default anyway. In the minibuffer, both RET and C-j terminate your minibuffer input and accept (read) it.

Third, your question is presumably what command RET is bound to in the minibuffer (by default). It can depend on which minibuffer keymap is currently active. But you can find out what it is bound to by examining the minibuffer keymaps.

To see the keys bound in a given minibuffer keymap, load library help-fns+.el and use M-k. When it asks you for a keymap variable, type one of the minibuffer keymap variable names. The names all start with minibuffer-local-, so you can type that and hit TAB for completion.

The main (base) keymap is minibuffer-local-map. The other maps generally inherit its key bindings and supplement them with other bindings. In minibuffer-local-map, RET is bound by default to exit-minibuffer. But in minibuffer-local-must-match-map, RET is bound by default to minibuffer-complete-and-exit. You get the idea.

Drew
  • 75,699
  • 9
  • 109
  • 225
3

To see what command a key is bound to at any point, use the key help commands. Type C-h c RET to see what RET is bound to. In a minibuffer, that's minibuffer-complete-and-exit. Because a minibuffer is active, the message will only appear for a brief time; switch to the *Messages* buffer if you haven't had time to read it.

The full message you'll see is

RET (translated from <return>) runs the command minibuffer-complete-and-exit

See Drew's answer for a throughout explanation of the bit about “RET (translated from <return>)”.

If you want to issue the newline command, you can look for a binding to it: type C-h w newline RET. You'll be told that

newline is not on any key

So the only way to run the newline command would be to execute it by name: M-x newline RET. But this requires a second minibuffer to type the command name, and by default Emacs won't let you do that, because having multiple active minibuffers can get confusing. Set the variable enable-recursive-minibuffers to allow it ((setq enable-recursive-minibuffers t) in your init file, or M-x set-variable RET enable-recursive-minibuffers RET t RET to enable it for this session).

If you want to insert a newline in a minibuffer, use the quoted-insert command. But you need to type C-q C-j, not C-q RET, because a newline in Emacs (like in most of the computer world today) is a line feed character, which is what C-j is, rather than a carriage return character, which is what RET (an alias for C-m) is.