Tuesday, December 9, 2008

Using Autocompletion in Ubuntu

The Tab key is your best friend when using the shell, because it will cause BASH to automatically complete whatever you type. For example, if you want to run Ubuntu’s web browser, you can enter firefox at the command line. However, to save yourself some time, you can type fir, 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.

Of course, autocompletion has some limitations. On my Ubuntu test system, typing loc didn’t autocomplete locate. Instead, it caused BASH to beep. This is because on a default Ubuntu installation, there is more than one possible match. Pressing Tab again shows those matches. 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 and slash tell 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 we mentioned earlier, if BASH cannot find a match, pressing Tab again causes BASH to show all the available options. 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, this produces the following list of commands:

badblocks banner baobab 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 you apply this trick 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 retrieving 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.

Source of Information : Apress Beginning Ubuntu Linux 3rd Edition

No comments: