预计阅读本页时间:-
Textual Completion
One of the most powerful (and typically underused) features of emacs-mode is its textual completion facility, inspired by similar features in the full emacs editor, the C shell, and (originally) the old DEC TOPS-20 operating system.
The premise behind textual completion is simple: you should have to type only as much of a filename, user name, function, etc., to identify it unambiguously. This is an excellent feature; there is an analogous one in vi-mode. We recommend that you take the time to learn it, since it will save you quite a bit of typing.
广告:个人专属 VPN,独立 IP,无限流量,多机房切换,还可以屏蔽广告和恶意软件,每月最低仅 5 美元
There are three commands in emacs-mode that relate to textual completion. The most important is TAB.[4] When you type in a word of text followed by TAB, bash will attempt to complete the name. Then one of four things can happen:
- If there is nothing whose name begins with the word, the shell will beep and nothing further will happen.
- If there is a command name in the search path, a function name, or a filename that the string uniquely matches, the shell will type the rest of it, followed by a space in case you want to type in more command arguments. Command name completion is only attempted when the word is in a command position (e.g., at the start of a line).
- If there is a directory that the string uniquely matches, the shell will complete the filename, followed by a slash.
- If there is more than one way to complete the name, the shell will complete out to the longest common prefix among the available choices. Commands in the search path and functions take precedence over filenames.
For example, assume you have a directory with the files tweedledee.c and tweedledum.c. You want to compile the first of these by typing cc tweedledee.c. You type cc twee followed by TAB. This is not an unambiguous prefix, since the prefix "twee" is common to both filenames, so the shell only completes out to cc tweedled. You need to type more letters to distinguish between them, so you type e and hit TAB again. Then the shell completes out to "cc tweedledee.c", leaving the extra space for you to type in other filenames or options.
If you didn't know what options were available after trying to complete cc twee, you could press TAB again. bash prints out the possible completions for you and presents your input line again:
$ cc tweedled
tweedledee.c tweedledum.c
$ cc tweedled
A related command is ESC-?, which expands the prefix to all possible choices, listing them to standard output. Be aware that the completion mechanism doesn't necessarily expand to a filename. If there are functions and commands that satisfy the string you provide, the shell expands those first and ignores any files in the current directory. As we'll see, you can force completion to a particular type.
It is also possible to complete other environment entities. If the text being completed is preceded by a dollar sign ($), the shell attempts to expand the name to that of a shell variable (see Chapter 3, for a discussion of shell variables). If the text is preceded by a tilde (~), completion to a username is attempted; if preceded by an at sign (@), a hostname is attempted.
For example, suppose there was a username cameron on the system. If you wanted to change to this user's home directory, you could just use tilde notation and type the first few letters of the name, followed by a TAB:
$ cd ~ca
which would expand to:
$ cd ~cameron/
You can force the shell to complete to specific things. Table 2-5 lists the standard keys for these.
Table 2-5. Completion command
Command
Description
TAB
Attempt to perform general completion of the text
ESC-?
List the possible completions
ESC-/
Attempt filename completion
CTRL-X /
List the possible filename completions
ESC-~
Attempt username completion
CTRL-X ~
List the possible username completions
ESC-$
Attempt variable completion
CTRL-X $
List the possible variable completions
ESC-@
Attempt hostname completion
CTRL-X @
List the possible hostname completions
ESC-!
Attempt command completion
CTRL-X !
List the possible command completions
ESC-TAB
Attempt completion from previous commands in the history list
If you find that you are interested only in completing long filenames, you are probably better off using ESC-/ rather than TAB. This ensures that the result will be a filename and not a function or command name.