The Minibuffer
**************

The minibuffer is where Emacs commands read complicated arguments, such as file names, buffer names, Emacs command names, or Lisp expressions.  We call it the ``minibuffer'' because it's a special-purpose buffer with a small amount of screen space.  You can use the usual Emacs editing commands in the minibuffer to edit the argument text.

When the minibuffer is in use, it appears in the echo area, with a cursor.  The minibuffer display starts with a prompt in a distinct color, usually ending with a colon.  The prompt states what kind of input is expected, and how it will be used.

The simplest way to enter a minibuffer argument is to type the text, then RET to submit the argument and exit the minibuffer.  You can cancel the minibuffer, and the command that wants the argument, by typing C-g.

Sometimes, a default argument appears in the prompt, inside parentheses before the colon.  The default will be used as the argument value if you just type RET.  For example, commands that read buffer names usually show a buffer name as the default; you can type RET to operate on that default buffer.

Since the minibuffer appears in the echo area, it can conflict with other uses of the echo area.  If an error occurs while the minibuffer is active, the error message hides the minibuffer for a few seconds, or until you type something; then the minibuffer comes back.  If a command such as C-x = needs to display a message in the echo area, the message hides the minibuffer for a few seconds, or until you type something; then the minibuffer comes back.  While the minibuffer is in use, keystrokes do not echo.

Minibuffers for File Names
==========================

Commands such as C-x C-f (find-file) use the minibuffer to read a file name argument (see Basic Files).  When the minibuffer is used to read a file name, it typically starts out with some initial text ending in a slash.  This is the default directory.  For example, it may start out like this:

     Find File: /u2/emacs/src/

Here, Find File:  is the prompt and /u2/emacs/src/ is the default directory.  If you now type buffer.c as input, that specifies the file /u2/emacs/src/buffer.c.  See File Names, for information about the default directory.

You can specify the parent directory by adding ..: for example, /u2/emacs/src/../lisp/simple.el is equivalent to /u2/emacs/lisp/simple.el.  Alternatively, you can use M-DEL to kill directory names backwards (see Words).

To specify a file in a completely different directory, you can kill the entire default with C-a C-k (see Minibuffer Edit). Alternatively, you can ignore the default, and enter an absolute file name starting with a slash or a tilde after the default directory. For example, you can specify /etc/termcap as follows:

     Find File: /u2/emacs/src//etc/termcap

Emacs interprets a double slash as ``ignore everything before the second slash in the pair.''  In the example above, /u2/emacs/src/ is ignored, so the argument you supplied is /etc/termcap.  The ignored part of the file name is dimmed if the terminal allows it (to disable this dimming, turn off File Name Shadow mode with the command M-x file-name-shadow-mode.)

Emacs interprets ~/ as your home directory.  Thus, ~/foo/bar.txt specifies a file named bar.txt, inside a directory named foo, which is in turn located in your home directory.  In addition, ~USER-ID/ means the home directory of a user whose login name is USER-ID.  Any leading directory name in front of the ~ is ignored: thus, /u2/emacs/~/foo/bar.txt is equivalent to ~/foo/bar.txt.

On MS-Windows and MS-DOS systems, where a user doesn't always have a home directory, Emacs uses several alternatives.  For MS-Windows, see Windows HOME; for MS-DOS, see HOME on MS-DOS. On these systems, the ~USER-ID/ construct is supported only for the current user, i.e., only if USER-ID is the current user's login name.

To prevent Emacs from inserting the default directory when reading file names, change the variable insert-default-directory to nil.  In that case, the minibuffer starts out empty. Nonetheless, relative file name arguments are still interpreted based on the same default directory.

Editing in the Minibuffer
=========================

The minibuffer is an Emacs buffer, albeit a peculiar one, and the usual Emacs commands are available for editing the argument text. (The prompt, however, is read-only, and cannot be changed.)

Since RET in the minibuffer is defined to exit the minibuffer, you can't use it to insert a newline in the minibuffer.  To do that, type C-o or C-q C-j.  (The newline character is really the ASCII character control-J.)

Inside a minibuffer, the keys TAB, SPC, and ? are often bound to commands that perform completion.  See Completion.  You can use C-q (quoted-insert) to insert a TAB, SPC, or ? character.  For example, C-q TAB inserts a TAB character.  See Inserting Text.

For convenience, C-a (move-beginning-of-line) in a minibuffer moves point to the beginning of the argument text, not the beginning of the prompt.  For example, this allows you to erase the entire argument with C-a C-k.

When the minibuffer is active, the echo area is treated much like an ordinary Emacs window.  For instance, you can switch to another window (with C-x o), edit text there, then return to the minibuffer window to finish the argument.  You can even kill text in another window, return to the minibuffer window, and yank the text into the argument.  There are some restrictions on the minibuffer window, however: for instance, you cannot split it.  See Windows.

Normally, the minibuffer window occupies a single screen line. However, if you add two or more lines' worth of text into the minibuffer, it expands automatically to accomodate the text.  The variable resize-mini-windows controls the resizing of the minibuffer.  The default value is grow-only, which means the behavior we have just described.  If the value is t, the minibuffer window will also shrink automatically if you remove some lines of text from the minibuffer, down to a minimum of one screen line.  If the value is nil, the minibuffer window never changes size automatically, but you can use the usual window-resizing commands on it (see Windows).

The variable max-mini-window-height controls the maximum height for resizing the minibuffer window.  A floating-point number specifies a fraction of the frame's height; an integer specifies the maximum number of lines; nil means do not resize the minibuffer window automatically.  The default value is 0.25.

The C-M-v command in the minibuffer scrolls the help text from commands that display help text of any sort in another window.  You can also scroll the help text with M-prior and M-next (or, equivalently, M-PageUp and M-PageDown).  This is especially useful with long lists of possible completions.  See Other Window.

Emacs normally disallows most commands that use the minibuffer while the minibuffer is active.  To allow such commands in the minibuffer, set the variable enable-recursive-minibuffers to t.

Completion
==========

Sometimes, you can use a feature called completion to help you enter arguments.  This means that after you type part of the argument, Emacs can fill in the rest, or some of it, based on what you have typed so far.

When completion is available, certain keys (usually TAB, RET, and SPC) are rebound to complete the text in the minibuffer into a longer string chosen from a set of completion alternatives.  The set of completion alternatives depends on the command that requested the argument, and on what you have typed so far.  In addition, you can usually type ? to display a list of possible completions.

For example, M-x uses the minibuffer to read the name of a command, so completion works by matching the minibuffer text against the names of existing Emacs commands.  So, to run the command insert-buffer, you can type M-x ins SPC b RET instead of the full M-x insert-buffer RET.

Case is significant in completion when it is significant in the argument you are entering, such as command names.  Thus, insert-buffer is not a valid completion for IN. Completion ignores case distinctions for certain arguments in which case does not matter.

Completion Example
------------------

A concrete example may help here.  If you type M-x a u TAB, the TAB looks for alternatives (in this case, command names) that start with au.  There are several, including auto-fill-mode and autoconf-mode, but they all begin with auto, so the au in the minibuffer completes to auto.

If you type TAB again immediately, it cannot determine the next character; it could be -, a, or c.  So it does not add any characters; instead, TAB displays a list of all possible completions in another window.

Next, type - f.  The minibuffer now contains auto-f, and the only command name that starts with this is auto-fill-mode.  If you now type TAB, completion fills in the rest of the argument auto-fill-mode into the minibuffer. You have been able to enter auto-fill-mode by typing just a u TAB - f TAB.

Completion Commands
-------------------

Here is a list of the completion commands defined in the minibuffer when completion is allowed.

TAB
     Complete the text in the minibuffer as much as possible; if unable to complete, display a list of possible completions (minibuffer-complete).

SPC
     Complete up to one word from the minibuffer text before point (minibuffer-complete-word).  SPC for completion is not available when entering a file name, since file names often include spaces.

RET
     Submit the text in the minibuffer as the argument, possibly completing first as described in the next node (minibuffer-complete-and-exit).  See Strict Completion.

?
     Display a list of possible completions of the text before point (minibuffer-completion-help).

TAB (minibuffer-complete) is the most fundamental completion command.  It searches for all possible completion alternatives that match the existing minibuffer text, and attempts to complete as much as it can.  The matching of completion alternatives to the minibuffer text is performed according to somewhat intricate rules, which are designed so that plausible completions are offered under most circumstances.  A valid completion alternative must satisfy the following criteria:

  * The minibuffer text before point must be the same as the beginning of the completion alternative.  If there is any minibuffer text after point, it must be a substring of the remainder of the completion alternative.

  * If no completion alternative satisfies the above rules, try using partial completion rules: divide the minibuffer text into words separated by hyphens or spaces, and complete each word separately. Thus, when completing command names, em-l-m completes to emacs-lisp-mode.

  * If there is still no completion alternative, try the first rule again, but ignore the minibuffer text after point (i.e., don't try matching it).

When performing these comparisons, a * in the minibuffer text acts as a wildcard---it matches any character at the corresponding position in the completion alternative.

SPC (minibuffer-complete-word) completes like TAB, but only up to the next hyphen or space.  If you have auto-f in the minibuffer and type SPC, it finds that the completion is auto-fill-mode, but it only inserts ill-, giving auto-fill-.  Another SPC at this point completes all the way to auto-fill-mode.

If TAB or SPC is unable to complete, it displays a list of possible completions (if there are any) in a separate window.  You can choose a completion from this list using the following commands:

Mouse-1
Mouse-2
     Clicking mouse button 1 or 2 on a completion possibility chooses that completion (mouse-choose-completion).

M-v
PageUp
prior
     Typing M-v, while in the minibuffer, selects the window showing the completion list buffer (switch-to-completions).  This paves the way for using the commands below.  Typing PageUp or prior does the same, as does selecting that window in other ways.

RET
     Typing RET, while in the completion list buffer, chooses the completion that point is in or next to (choose-completion).  To use this command, you must first switch to the completion list window.

Right
     Typing the right-arrow key Right, while in the completion list buffer, moves point to the following completion possibility (next-completion).

Left
     Typing the left-arrow key Left, while in the completion list buffer, moves point to the previous completion possibility (previous-completion).

Strict Completion
-----------------

There are three different ways that RET can do completion, depending on how the argument will be used.

  * Strict completion accepts only known completion candidates.  For example, when C-x k reads the name of a buffer to kill, only the name of an existing buffer makes sense.  In strict completion, RET refuses to exit if the text in the minibuffer does not complete to an exact match.

  * Cautious completion is similar to strict completion, except that RET exits only if the text is an already exact match. Otherwise, RET does not exit, but it does complete the text.  If that completes to an exact match, a second RET will exit.

     Cautious completion is used for reading file names for files that must already exist, for example.

  * Permissive completion allows any input; the completion candidates are just suggestions.  For example, when C-x C-f reads the name of a file to visit, any file name is allowed, including nonexistent file (in case you want to create a file).  In permissive completion, RET does not complete, it just submits the argument as you have entered it.

The completion commands display a list of all possible completions whenever they can't determine even one more character by completion. Also, typing ? explicitly requests such a list.  You can scroll the list with C-M-v (see Other Window).

Completion Options
------------------

If completion-auto-help is set to nil, the completion commands never display the completion list buffer; you must type ?  to display the list.  If the value is lazy, Emacs only shows the completion list buffer on the second attempt to complete. In other words, if there is nothing to complete, the first TAB echoes Next char not unique; the second TAB does the completion list buffer.

When completing file names, certain file names are usually ignored. The variable completion-ignored-extensions contains a list of strings; a file name ending in any of those strings is ignored as a completion candidate.  The standard value of this variable has several elements including ".o", ".elc", and "~".  For example, if a directory contains foo.c and foo.elc, foo completes to foo.c.  However, if all possible completions end in ``ignored'' strings, they are not ignored: in the previous example, foo.e completes to foo.elc. Displaying a list of possible completions disregards completion-ignored-extensions; it shows them all.

If an element of completion-ignored-extensions ends in a slash (/), it's a subdirectory name; that directory and its contents are ignored.  Elements of completion-ignored-extensions that do not end in a slash are ordinary file names.

When completing file names, Emacs ignores case differences if the variable read-file-name-completion-ignore-case is non-nil.  The default value is nil on systems that have case-sensitive file-names, such as GNU/Linux; it is non-nil on systems that have case-insensitive file-names, such as Microsoft Windows.  When completing buffer names, Emacs ignores case differences if read-buffer-completion-ignore-case is non-nil (the default value is nil).

You can customize the matching rules for completion alternatives using the variable completion-styles.  Its value should be a list of symbols, each representing a completion style; valid style symbols are basic, partial-completion, emacs22, and emacs21.  When completing, Emacs attempts to use the first completion style in the list; if this does not return any completion alternatives, it tries the next completion style in the list, and so on.  The completion rules described in Completion Commands correspond to the default value of completion-styles, which is (basic partial-completion emacs22).

Icomplete mode presents a constantly-updated display that tells you what completions are available for the text you've entered so far.  The command to enable or disable this minor mode is M-x icomplete-mode.

Minibuffer History
==================

Every argument that you enter with the minibuffer is saved in a minibuffer history list so you can easily use it again later. You can use the following arguments to quickly fetch an earlier argument into the minibuffer:

M-p
Up
     Move to the previous item in the minibuffer history, an earlier argument (previous-history-element).

M-n
Down
     Move to the next item in the minibuffer history (next-history-element).

M-r REGEXP RET
     Move to an earlier item in the minibuffer history that matches REGEXP (previous-matching-history-element).

M-s REGEXP RET
     Move to a later item in the minibuffer history that matches REGEXP (next-matching-history-element).

While in the minibuffer, typing M-p or Up (previous-history-element) moves up through the minibuffer history list, one item at a time.  Each M-p fetches an earlier item from the history list into the minibuffer, replacing its existing contents.  Similarly, typing M-n or Down (next-history-element) moves back down the history list, fetching later entries into the minibuffer.  You can think of these commands as ``backwards'' and ``forwards'' through the history list.

If you type M-n in the minibuffer when there are no later entries in the minibuffer history (e.g., if you haven't previously typed M-p), Emacs tries fetching from a list of default argument: values that you are likely to enter.  You can think of this as moving through the ``future list'' instead of the ``history list''.

The input that M-p or M-n fetches into the minibuffer entirely replaces the existing contents of the minibuffer, so you can simply type RET to use it as an argument.  You can also edit the text before you reuse it; this does not change the history element that you ``moved'' to, but your new argument does go at the end of the history list in its own right.

There are also commands to search forward or backward through the history; they search for history elements that match a regular expression.  M-r (previous-matching-history-element) searches older elements in the history, while M-s (next-matching-history-element) searches newer elements.  These commands are unusual: they use the minibuffer to read the regular expression even though they are invoked from the minibuffer.  As with incremental searching, an upper-case letter in the regular expression makes the search case-sensitive (see Search Case).  You can also search through the history using an incremental search (see Isearch Minibuffer).

All uses of the minibuffer record your input on a history list, but there are separate history lists for different kinds of arguments. For example, there is a list for file names, used by all the commands that read file names.  (As a special feature, this history list records the absolute file name, even if the name you entered was not absolute.)

There are several other specific history lists, including one for buffer names, one for arguments of commands like query-replace, one used by M-x for command names, and one used by compile for compilation commands.  Finally, there is one ``miscellaneous'' history list that most minibuffer arguments use.

The variable history-length specifies the maximum length of a minibuffer history list; adding a new element deletes the oldest element if the list gets too long.  If the value of history-length is t, there is no maximum length.

The variable history-delete-duplicates specifies whether to delete duplicates in history.  If it is non-nil, adding a new element deletes from the list all other elements that are equal to it. The default is nil.

Repeating Minibuffer Commands
=============================

Every command that uses the minibuffer once is recorded on a special history list, the command history, together with the values of its arguments, so that you can repeat the entire command.  In particular, every use of M-x is recorded there, since M-x uses the minibuffer to read the command name.

C-x ESC ESC
     Re-execute a recent minibuffer command from the command history (repeat-complex-command).

M-x list-command-history
     Display the entire command history, showing all the commands C-x ESC ESC can repeat, most recent first.

C-x ESC ESC is used to re-execute a recent command that used the minibuffer.  With no argument, it repeats the last such command.  A numeric argument specifies which command to repeat; 1 means the last one, 2 the previous, and so on.

C-x ESC ESC works by turning the previous command into a Lisp expression and then entering a minibuffer initialized with the text for that expression.  Even if you don't understand Lisp syntax, it will probably be obvious which command is displayed for repetition.  If you type just RET, that repeats the command unchanged.  You can also change the command by editing the Lisp expression before you execute it.  The repeated command is added to the front of the command history unless it is identical to the most recently item.

Once inside the minibuffer for C-x ESC ESC, you can use the minibuffer history commands (M-p, M-n, M-r, M-s; see Minibuffer History) to move through the history list of saved entire commands.  After finding the desired previous command, you can edit its expression as usual and then repeat it by typing RET.

Incremental search does not, strictly speaking, use the minibuffer. Therefore, although it behaves like a complex command, it normally does not appear in the history list for C-x ESC ESC. You can make incremental search commands appear in the history by setting isearch-resume-in-command-history to a non-nil value.  See Incremental Search.

The list of previous minibuffer-using commands is stored as a Lisp list in the variable command-history.  Each element is a Lisp expression which describes one command and its arguments.  Lisp programs can re-execute a command by calling eval with the command-history element.

Entering passwords
==================

Sometimes, you may need to enter a password into Emacs.  For instance, when you tell Emacs to visit a file on another machine via a network protocol such as FTP, you often need to supply a password to gain access to the machine (see Remote Files).

Entering a password is, in a basic sense, similar to using a minibuffer.  Emacs displays a prompt in the echo area (such as Password: ); after you type the required password, press RET to submit it.  To prevent others from seeing your password, every character you type is displayed as a dot (.) instead of its usual form.

Most of the features and commands associated with the minibuffer can not be used when entering a password.  There is no history or completion, and you cannot change windows or perform any other action with Emacs until you have submitted the password.

While you are typing the password, you may press DEL to delete backwards, removing the last character entered.  C-u deletes everything you have typed so far.  C-g quits the password prompt (see Quitting).  C-y inserts the current kill into the password (see Killing).  You may type either RET or ESC to submit the password.  Any other self-inserting character key inserts the associated character into the password, and all other input is ignored.