14.5 Completion
We saw an example of Emacs's completion facility in Chapter 1. Completion is more than just a feature: it is a general principle in the design of Emacs. It can be articulated as follows:
If you have to type in the name of something, and that name is one of a finite number of possibilities, Emacs should figure out what you mean after the smallest possible number of keystrokes.
In other words, you can type in the shortest unambiguous prefix and tell Emacs to figure out the rest of the name. By "shortest unambiguous prefix," we mean "enough of the name, starting from the beginning, to distinguish it from the other possibilities." Several important things in Emacs have names that are chosen from a finite number of possibilities, including the following:
• Commands
• Files in a given directory
• Buffers
• Emacs variables
Most of the time, completion is available when you are prompted for a name of something in the minibuffer. While you are typing in the name, you can use three keys to tell Emacs to help complete it for you: Tab, Space, and question mark (?). Their functions are shown in Table 14-5.
Table 14-5. Completion keys
| Keystroke | Action |
|---|---|
| Tab | Completes the name as far as possible. |
| Space | Completes the name out to the next punctuation character. |
| ? | Lists the choices at this point in a *Completions* window. |
You will probably find Tab to be the most useful.
As a running example, assume you have typed C-x C-f to visit a file, and the file you want to visit is a C program called program.c. Let's say you type pro and press Tab; Emacs responds by completing the name to the full program.c. If you press Space, Emacs completes only as far as program. After Emacs completes the name, you can press Enter to visit the file.
How much of the name do you need to type in before you can use completion? That depends on the other possible choices in the given situation. If program.c were the only file in your directory, you could just type p and press Tab.[98] If there were other files in your directory and none of them has a name beginning with p, you could do the same thing. But if you had a file called problem.c, you would have to type prog before you pressed Tab; in this case, prog is the shortest unambiguous prefix. If you just type in pro and press Tab, Emacs responds with a *Completions* window containing a list of the completion choices, in this case program.c and problem.c, and returns your cursor to the minibuffer so that you can finish typing the filename. The same thing happens if you typed a question mark instead of Tab. At this point, you can type g and press Tab again; Emacs completes the name to program.c.
98
You can't just press Tab without typing the p because the current and parent directories, named