Using Autocompletion


The Tab key is your best friend when using the shell. It will cause BASH to automatically complete whatever you type. For example, if you want to type konqueror, you can type konq, and then press Tab. You'll then find that BASH fills in the rest for you. It does this by caching the names of the programs you might run according to the directories listed in your $PATH variable (see Chapter 14).

Of course, there are limitations to autocompletion. On my SUSE Linux test system, typing loc didn't autocomplete locate (which I know is installed). Instead, it caused BASH to beep. This is because, on a default SUSE Linux installation, there is more than one possible match. Pressing Tab again shows what they are. Depending on how much you type (how much of an initial clue you give BASH), you might find there are many possible matches.

In this case, the experienced BASH user simply types another letter, which will be enough to distinguish the almost-typed word from the rest, and presses Tab again. With any luck, this should be enough for BASH to fill in the rest.

Autocompletion with Files and Paths

Tab autocompletion also works with files and paths. If you type the first few letters of a folder name, BASH will try to fill in the rest. This also obviously has limitations. There's no point in typing cd myfol and pressing Tab if there's nothing in the current directory that starts with the letters myfol. This particular autocomplete function works by looking at your current directory and seeing what's available.

Alternatively, you can specify an initial path for BASH to use in order to autocomplete. Typing cd /ho and pressing Tab will cause BASH to autocomplete the path by looking in the root directory (/). In other words, it will autocomplete the command with the directory home.

In a similar way, typing cd myfolder/myfo will cause BASH to attempt to autocomplete by looking for a match in myfolder.

If you want to run a program that resides in the current directory, such as one you've just downloaded for example, typing ./, followed by the first part of the program name, and then pressing Tab should be enough to have BASH autocomplete the rest. In this case, the dot slash tells BASH to look in the current directory for any executable programs or scripts (programs with X as part of their permissions) and use them as possible autocomplete options.

BASH is clever enough to spot whether the command you're using is likely to require a file, directory, or executable, and it will autocomplete with only relevant file or directory names.

Viewing Available Options

The autocomplete function has a neat side effect. As I mentioned earlier, if BASH cannot find a match, the dot slash tells BASH to show what all the available options are. For example, typing ba at the shell, and then pressing Tab twice will cause BASH to show all the possible commands starting with the letters ba.

On my test PC, doing this produces the following list of commands:

basename    bash bashbug     batch

This can be a nice way of exploring what commands are available on your system. You can then use each command with the --help command option to find out what it does, or browse the command's man page.

When this trick is applied to directory and filename autocompletion, it's even more useful. For example, typing cd in a directory, and then pressing the Tab key twice will cause BASH to show the available directories, providing a handy way of getting a brief directory listing. Alternatively, if you've forgotten how a directory name is spelled, you can use this technique to find out prior to switching into it.

Figure 18-1 shows a few examples of using this technique with BASH.

image from book
Figure 18-1. Autocompletion makes using BASH much easier.




Beginning SUSE Linux from Novice to Professional
Beginning SUSE Linux: From Novice to Professional
ISBN: 1590594584
EAN: 2147483647
Year: 2005
Pages: 293
Authors: Keir Thomas

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net