268 CHAPTER 17 COOL SHELL TRICKS Autocompletion (Web site traffic)
268 CHAPTER 17 COOL SHELL TRICKS 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 myfoland 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/myfowill 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 Xas 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, pressing Tab again causes BASH to show all the available options. For example, typing baat 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 base-config bash batch banner basename bashbug 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. Figure 17-1 shows a few examples of using this technique with BASH.