Free Hosting Online for WorkStations

< Previous | Contents | Next >

Completion

Another way that the shell can help you is through a mechanism called completion. Com- pletion occurs when you press the tab key while typing a command. Let's see how this

Completion


works. Given a home directory that looks like this:



[me@linuxbox ~]$ ls

Desktop ls-output.txt

Pictures Templates

Videos

Documents Music

Public


Try typing the following but don't press the Enter key:


[me@linuxbox ~]$ ls l

[me@linuxbox ~]$ ls l


Now press the tab key:



[me@linuxbox ~]$ ls ls-output.txt

[me@linuxbox ~]$ ls ls-output.txt


See how the shell completed the line for you? Let's try another one. Again, don't press

Enter:


[me@linuxbox ~]$ ls D

[me@linuxbox ~]$ ls D


Press tab:



[me@linuxbox ~]$ ls D

[me@linuxbox ~]$ ls D


No completion, just a beep. This happened because “D” matches more than one entry in the directory. For completion to be successful, the “clue” you give it has to be unambigu- ous. If we go further:



[me@linuxbox ~]$ ls Do

[me@linuxbox ~]$ ls Do


Then press tab:



[me@linuxbox ~]$ ls Documents

[me@linuxbox ~]$ ls Documents


The completion is successful.

While this example shows completion of pathnames, which is its most common use, completion will also work on variables (if the beginning of the word is a “$”), user names (if the word begins with “~”), commands (if the word is the first word on the line) and hostnames (if the beginning of the word is “@”). Hostname completion only works for hostnames listed in /etc/hosts.

There are a number of control and meta key sequences that are associated with comple- tion:


Table 8-4: Completion Commands


Key Action

Key Action

Alt-? Display list of possible completions. On most systems you can also do this by pressing the tab key a second time, which is much easier.


Alt-* Insert all possible completions. This is useful when you want to use more than one possible match.



There quite a few more that I find rather obscure. You can see a list in the bash man page under “READLINE”.


Programmable Completion

Recent versions of bash have a facility called programmable completion. Pro- grammable completion allows you (or more likely, your distribution provider) to add additional completion rules. Usually this is done to add support for specific applications. For example, it is possible to add completions for the option list of a command or match particular file types that an application supports. Ubuntu has a fairly large set defined by default. Programmable completion is implemented by shell functions, a kind of mini shell script that we will cover in later chapters. If you are curious, try:

set | less

and see if you can find them. Not all distributions include them by default.


Top OS Cloud Computing at OnWorks: