3.5. Command-line Completion

Completion is a very handy functionality, 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.

3.5.1. Example

Suppose your personal directory contains a file which name is file_with_very_long_name_impossible_to_type, 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 you 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 sequence of keys:

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.

3.5.2. Other Completion Methods

The TAB key is not the only way to activate completion, although it is the most common. 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[1]. There is also a magic character for completing a command name (!) or a file name (/).

The other two ways to activate completion are the sequences Esc-<x> and C-x <x> (Esc being the Escape key, and C-x meaning Control+<x>), where <x> is one of the magic characters already mentioned. Esc-<x> will attempt to come up with a unique completion, and if it fails will complete the word with the largest possible substring in the list of choices. A beep means either that the choice is not unique, or simply that there is no corresponding choice. The sequence C-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 C-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 C-x $ in a blank line. Another example: if you want to see the page of the manual for the command nslookup, you simply type man nsl then Esc-!, and the shell will automatically complete the command to man nslookup.

Notes

[1]

Remember: Unix differentiates between uppercase and lowercase. The HOME environment variable and the home variable are not the same.


Tux on Star from MandrakeSoft Linux is a registered trademark of Linus Torvalds. All other trademarks and copyrights are the property of their respective owners.
Unless otherwise stated, all the content of these pages and all images are Copyright MandrakeSoft S.A. and MandrakeSoft Inc. 2000.
http://www.linux-mandrake.com/