Completion is a very handy function,
and all modern shells
(including bash) have it. Its
role is to give the user as little work to do as possible. The best way to
illustrate completion is to give an example.
Suppose your personal directory contains
the file_with_very_long_name_impossible_to_type
file, and you want to look at it. Suppose you also have, in the same
directory, another file called file_text
. You are
in your personal directory, so type the following sequence:
$ less fi<TAB>
(i.e., type
less fi and then press the
Tab key). The shell
will then expand the
command line for you:
$ less file_
and also give the list of possible choices (in its default configuration, which can be customized). Then type the following key sequence:
less file_w<TAB>
and the shell
will extend the
command line to give you the result you want:
less file_with_very_long_name_impossible_to_type
All you need to do then is press the Enter key to confirm and read the file.
The Tab key is not the only way to
activate completion, although it is the most common one. As a general
rule, the word to be completed will be a command name for the first word
of the command line (nsl<TAB> will give
nslookup), and a file name for all the others, unless the
word is preceded by a “magic” character like
~
, @
or $
, in
which case the shell
will try to complete a user name, a
machine name or an environment variable name respectively[8]. There is also a magic character for completing a file
name (/
) and a command to recall a command from the
history (!
).
The other two ways to activate completion are the sequences Esc-<x> and Ctrl-X-<x>, where <x> is one of the magic characters already mentioned. Esc-<x> will attempt to come up with a unique completion. If it fails, it will complete the word with the largest possible substring in the choice list. A beep means either that the choice is not unique, or simply that there is no corresponding choice. The sequence Ctrl-X-<x> displays the list of possible choices without attempting any completion. Pressing the Tab key is the same as successively pressing Esc-<x> and Ctrl-X-<x>, where the magic character depends on the context.
Thus, one way to see all the environment variables defined is to type the sequence Ctrl-X-$ on a blank line. Another example: if you want to see the man page for the nslookup command, you simply type man nsl then Esc-!, and the shell will automatically complete the command to man nslookup.
[8] Remember: UNIX® differentiates
between uppercase and lowercase. The HOME
environment variable and the home
variable are
not the same.