[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9. The Minibuffer

The minibuffer is where Emacs commands read complicated arguments (anything more a single number). We call it the "minibuffer" because it's a special-purpose buffer with a small amount of screen space. Minibuffer arguments can be file names, buffer names, Lisp function names, Emacs command names, Lisp expressions, and many other things--whatever the command wants to read. 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; it says what kind of input is expected and how it will be used. Often the prompt is derived from the name of the command that is reading the argument. The prompt normally ends with a colon.

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 show a buffer name as the default. You can type RET to operate on that default buffer.

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

Since the minibuffer appears in the echo area, it can conflict with other uses of the echo area. Here is how Emacs handles such conflicts:


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.1 Minibuffers for File Names

When you use the minibuffer to enter a file name, it starts out with some initial text--the default directory, ending in a slash. The file you specify will be in this directory unless you alter or replace it.

For example, if the minibuffer starts out with these contents:

 
Find File: /u2/emacs/src/

(where `Find File: ' is the prompt), and you type buffer.c as input, that specifies the file `/u2/emacs/src/buffer.c'. You can specify the parent directory by adding `..'; thus, if you type ../lisp/simple.el, you will get `/u2/emacs/lisp/simple.el'. Alternatively, you can use M-DEL to kill the directory names you don't want (see section Words).

You can kill the entire default with C-a C-k, but there's no need to do that. It's easier to ignore the default, and enter an absolute file name starting with a slash or a tilde after the default directory. For example, to specify `/etc/termcap', just type that name:

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

GNU Emacs interprets a double slash (which is not normally useful in file names) as, "ignore everything before the second slash in the pair." In the example above. `/u2/emacs/src/' is ignored, so you get `/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 (a minor mode) with the command M-x file-name-shadow-mode.

If the variable insert-default-directory is nil, the default directory is never inserted in the minibuffer--so the minibuffer starts out empty. Nonetheless, relative file name arguments are still interpreted based on the same default directory.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.2 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.

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.)

The minibuffer has its own window, which normally has space in the frame at all times, but it only acts like an Emacs window when the minibuffer is active. When active, this window is much like any other 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 then yank the text into the argument. See section Multiple Windows.

There are some restrictions on the minibuffer window, however: you cannot kill it, or split it, or switch buffers in it--the minibuffer and its window are permanently attached.

The minibuffer window expands vertically as necessary to hold the text that you put in the minibuffer. If resize-mini-windows is t (the default), the window always resizes as needed by its contents. If its value is the symbol grow-only, the window grows automatically as needed, but shrinks (back to the normal size) only when the minibuffer becomes inactive. If its value is nil, you have to adjust the height yourself.

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. M-PAGEUP and M-PAGEDOWN also operate on that help text. This is especially useful with long lists of possible completions. See section Using Other Windows.

Emacs normally disallows most commands that use the minibuffer while the minibuffer is active. (Entering the minibuffer from the minibuffer can be confusing.) To allow such commands in the minibuffer, set the variable enable-recursive-minibuffers to t.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3 Completion

Some arguments allow completion to enter their value. 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--TAB, RET, and SPC--are rebound to complete the text in the minibuffer before point into a longer string chosen from a set of completion alternatives provided by the command that requested the argument. (SPC does not do completion in reading file names, because it is common to use spaces in file names on some systems.) ? displays a list of the possible completions at any time.

For example, M-x uses the minibuffer to read the name of a command, so it provides a list of all Emacs command names for completion candidates. The completion keys match the minibuffer text against these candidates, find any additional name characters implied by the text already present in the minibuffer, and add those characters. This makes it possible to type M-x ins SPC b RET instead of M-x insert-buffer RET, for example.

Case is significant in completion when it is significant in the argument you are entering (buffer names, file names, command names, for instance). Thus, `fo' does not complete to `Foo'. Completion ignores case distinctions for certain arguments in which case does not matter.

Completion acts only on the text before point. If there is text in the minibuffer after point--i.e., if you move point backward after typing some text into the minibuffer--it remains unchanged.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3.1 Completion Example

A concrete example may help here. If you type M-x au TAB, the TAB looks for alternatives (in this case, command names) that start with `au'. There are several, including auto-fill-mode and auto-save-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 any of `cfilrs'. So it does not add any characters; instead, TAB displays a list of all possible completions in another window.

Now type f TAB. This TAB sees `auto-f'. The only command name starting with that is auto-fill-mode, so completion fills in the rest of that. You have been able to enter `auto-fill-mode' by typing just au TAB f TAB.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3.2 Completion Commands

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

TAB

Complete the text before point in the minibuffer as much as possible (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 section Strict Completion.

?

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

SPC 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'. The command that implements this behavior is called minibuffer-complete-word.

When you display a list of possible completions, you can choose one from it:

Mouse-1
Mouse-2

Clicking mouse button 1 or 2 on a completion possibility chooses that completion (mouse-choose-completion). You must click in the list of completions, not in the minibuffer.

PRIOR
M-v

Typing PRIOR or PAGE-UP, or 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. (Selecting that window in other ways has the same effect.)

RET

Typing RET 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 in the completion list buffer moves point to the following completion possibility (next-completion).

LEFT

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


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3.3 Strict Completion

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

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 section Using Other Windows).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.3.4 Completion Options

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", ".dvi" and "~". The effect is that, for example, `foo' can complete to `foo.c' even though `foo.o' exists as well. However, if all the possible completions end in "ignored" strings, then they are not ignored. 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; then that directory and its contents are ignored. Elements of completion-ignored-extensions which do not end in a slash are ordinary file names, and do not apply to names of directories.

If completion-auto-help is set to nil, the completion commands never display a list of possibilities; you must type ? to display the list.

Partial Completion mode implements a more powerful kind of completion that can complete multiple words in parallel. For example, it can complete the command name abbreviation p-b into print-buffer if no other command starts with two words whose initials are `p' and `b'.

To enable this mode, use M-x partial-completion-mode, or customize the variable partial-completion-mode. This mode binds special partial completion commands to TAB, SPC, RET, and ? in the minibuffer. The usual completion commands are available on M-TAB (or C-M-i), M-SPC, M-RET and M-?.

Partial completion of directories in file names uses `*' to indicate the places for completion; thus, `/u*/b*/f*' might complete to `/usr/bin/foo'. For remote files, partial completion enables completion of methods, user names and host names. See section Remote Files.

Partial Completion mode also extends find-file so that `<include>' looks for the file named include in the directories in the path PC-include-file-path. If you set PC-disable-includes to non-nil, this feature is disabled.

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.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.4 Minibuffer History

Every argument that you enter with the minibuffer is saved on a minibuffer history list so you can easily use it again later. Special commands fetch the text of an earlier argument into the minibuffer, replacing the old minibuffer contents. You can think of them as moving through the history of previous arguments.

UP
M-p

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

DOWN
M-n

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).

To move through the minibuffer history list one item at a time, use M-p or up-arrow (previous-history-element) to fetch the next earlier minibuffer input, and use M-n or down-arrow (next-history-element) to fetch the next later input. These commands don't move the cursor, they pull different saved strings into the minibuffer. But you can think of them as "moving" through the history list.

The input that you fetch from the history entirely replaces the contents of the minibuffer. To use it again unchanged, just type RET. 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.

For many minibuffer arguments there is a "default" value. You can insert the default value into the minibuffer as text by using M-n. You can think of this as moving "into the future" in the history.

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 section Searching and Case).

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, though, there is no maximum length.

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


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

9.5 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 section 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 section 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.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by Mark Kaminski on July, 3 2008 using texi2html 1.70.