May 8, 2006

CLI Magic: Bash complete

Author: Shashank Sharma

The auto complete feature of the Bourne Again SHell makes bash one of the most loved and newbie-friendly Linux shells. Just by pressing the Tab key you can complete commands and filenames. Press the Tab key twice and all files in the directory get displayed. But you can do more with autocomplete -- such as associating file types with applications, and automatically designating whether you're looking for directories, text, or MP3 files. With simple commands such as complete and the use of Escape sequences, you can save time and have fun on the command line.You can use the dollar sign ($), tilde (~), and at (@) characters along with the Tab key to get quick results in autocomplete.

For instance, if you want to switch to the testing subdirectory of your home directory, you can either type cd /ho[Tab]/tes[Tab] to get there, or use the tilde -- cd ~tes[Tab]. If the partial text -- that is, the portion before you press Tab -- begins with a dollar sign, bash looks for a matching environment variable. The tilde tells bash to look for a matching user name, and the at-sign tells it to look for a matching hostname.

Escaping is good

The Tab key can complete the names of commands, files, directories, users, and hosts. Sometimes, it is overkill to use the Tab key. If you know that you are looking for a file, or only user names, then use the Escape key instead for completion, as it limits bash's completion field.

You can use several Escape key combinations to tell bash what you are looking for. Invoke Escape key combinations by pressing a key while keeping the Escape key pressed. When looking for a file, you can use the Esc-/ (press / along with Escape) key combination. This will attempt filename completion only. If you have one file and one directory beginning with the letter 'i,' you will have to press the Tab key twice to see all the files:

$ less i <tab><tab>
ideas im articles/

When you type less i and press '/' while keeping the Escape key pressed, bash completes the filename to 'ideas.'

While Control key combinations work no matter how long you keep the Ctrl key pressed before pressing the second key, this is not the case with Escape key sequences. The Esc-/ sequence will print out a slash if you delay in pressing the / key after you press the Escape key.

You can also use Escape along with the previously discussed $, ~, and @ keys. Esc-$, for example, completes only variable names. You can use Esc-! when you wish to complete command names. Of course you need to press the Shift key in order to use any of the "upper order" characters.

Wildcard expansion

The asterisk (*), caret (^), and question mark (?) are all wildcard characters. All *nix shells can perform wildcard expansion. Use the asterisk wildcard if you don't wish to view any hidden files. ls * would display all files and directories in the current directory except for those beginning with a dot (hidden).

If you wish to view only files with five-letter names beginning with a given letter, use the question mark wildcard. ls p???? will display only files with names with five letter files starting with 'p.' You can also use the square brackets for filename completion. ls [a-d]* displays all files and directories that begin with any letter between 'a' and 'd.' The caret wildcard excludes files. mv *[^.php] ../ would move all files to the parent directory, exculding those with .php extension.

Even smarter completion

By default, Tab completion is quite dim-witted. This is because when you have already typed cd down before pressing Tab, you'd expect bash to complete only directory names. But bash goes ahead and displays all possible files and directories that begin with 'down.'

You can, however, convert bash into a brilliant command-reading whiz. As root, edit the /etc/bash.bashrc file. Scroll down to the end of the file till you see the section:

# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ]; then
#    . /etc/bash_completion

Uncomment this section and voilĂ , you have given bash powers far beyond your imagination! Not only is bash now smart enough to know when to complete only directory names, it can also complete man pages and even some command arguments.

Don't despair if you don't have root previleges. Just edit the last section of your ~/.bashrc file.

Associating application with file types

The complete command in bash lets you associate file types with certain applications. If after associating a file type to an application you were to write the name of the application and press Tab, only files with associated file types would be displayed.

complete -G "*.txt" gedit would associate .txt files with gedit. The downfall of using complete is that it overwrites bash's regular completion. That is, if you have two files named invoice.txt and ideas.txt, gedit [Tab][Tab] displays both the files, but gedit inv[Tab], which should complete to invoice.txt, no longer works.

complete associations last only for the current bash session. If you exit and open a console, gedit will no longer be associated with .txt files. You need to associate file types to applications each time you start a new console session.

For permanent associations, you need to add the command to one of the bash startup scripts, such as ~/.bashrc. Then, whenever you are at the console, gedit will be associated with .txt files.

Shashank Sharma is studying for a degree in computer science. He specializes in writing about free and open source software for new users.


  • Linux
Click Here!