This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new Linux.com!

Linux.com

Feature: Linux

CLI Magic: Bash complete

By Shashank Sharma on May 08, 2006 (8:00:00 AM)

Share    Print    Comments   

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
#fi

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.

Shashank Sharma specializes in writing about free and open source software for new users and moderates the Linux.com forum boards. He is the coauthor of Beginning Fedora, published by Apress.

Share    Print    Comments   

Comments

on CLI Magic: Bash complete

Note: Comments are owned by the poster. We are not responsible for their content.

lower back pain

Posted by: Anonymous Coward on May 28, 2006 02:42 PM
<tt>[URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]
[URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]
[URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.c<nobr>o<wbr></nobr> m] Pain relief [/URL]
[URL=http://nervepainrelief.jeeran.com/pa<nobr>i<wbr></nobr> nrelief.htm] Nerve pain relief [/URL]</tt>

#

Wow

Posted by: Anonymous Coward on May 08, 2006 11:58 PM
Those were excellent tips! I'll try them as soon as I get home!


    []s Gus

#

Re:Wow

Posted by: Anonymous Coward on May 09, 2006 08:05 PM
yea, bash completion doesnt come with FC 3 or 4 that i know of, but do a

yum install bash-completion.noarch

and add the lines mentioned above to<nobr> <wbr></nobr>/etc/bashrc as root or to the user bashrc if you dont have root.

#

*[^.php], /etc/bash_completion

Posted by: Anonymous Coward on May 09, 2006 12:21 AM
*[^.php]
This will match any file not ending in '.', p or h.
I.e. it will exclude:
bash
grep
etc.

The [^ ] construct matches only one character.

Also, I don't seem to have<nobr> <wbr></nobr>/etc/bash_completion on my Fedora Core 3 box, maybe it's a Debian thing, or the Fedora maintainers removed it from the FC3 bash package perhaps.

#

Re:*[^.php], /etc/bash_completion

Posted by: Administrator on May 09, 2006 02:33 AM
Under slackware it's a separate package under extra/.



I don't think it's part of bash, so FC must have it as a separate package as well.

#

Re:*[^.php], /etc/bash_completion

Posted by: Administrator on May 09, 2006 03:32 AM
For FC5 (I suppose it is the same for previous version of FC), you just need to install the separate bash-completion package.

#

Re:*[^.php], /etc/bash_completion

Posted by: Anonymous Coward on May 09, 2006 03:43 AM
Thanks for this info, I've now found it in Extras...

Great article too.

#

Complete remote files

Posted by: Anonymous Coward on May 09, 2006 04:47 AM
Here's another great tip:
If you set up RSA authentication for SSH you can even auto-complete files on remote machines!
Example:

$ cp index.html webserver:/va[tab]w[tab]lo[tab]ht[tab]

(assuming webroot in webserver:/var/www/localhost/htdocs)

At least this works fine in Gentoo.

#

temporally disable

Posted by: Anonymous Coward on May 09, 2006 10:51 AM
The<nobr> <wbr></nobr>/etc/bash_completion looks great. But I was wondering it it can be disabled for a specific instance if you just really want normal filename completion.

#

Back Pain relief

Posted by: Anonymous Coward on May 28, 2006 01:53 PM
[URL=http://painrelief.fanspace.com/index.htm] Pain relief [/URL]

  [URL=http://lowerbackpain.0pi.com/backpain.htm] Back Pain [/URL]

  [URL=http://painreliefproduct.guildspace.com] Pain relief [/URL]
[URL=http://painreliefmedic.friendpages.com] Pain relief [/URL]
[URL=http://nervepainrelief.jeeran.com/painrelief<nobr>.<wbr></nobr> htm] Nerve pain relief [/URL]

#

bash completion?

Posted by: Anonymous Coward on May 09, 2006 01:34 PM
Well, if you're looking for a highly customizable completion system in a shell,
you should really checkout the zsh http://www.zsh.org/>.
It's by far the best shell for interactive use, IMHO.

#

Re:bash completion?

Posted by: Anonymous Coward on May 09, 2006 09:30 PM
Or <a href="http://roo.no-ip.org/fish" title="no-ip.org">fish</a no-ip.org>. By far the best shell for interactive use in _my_ humble opinion. But I am not objective since I'm the author of fish.

A few advantages fish has over over zsh:


  • When autocompleting commands, a brief description of what each command does is shown, when autocompleting manual pages, the whatis info on that page is shown. When autocompleting a process id, the name of the process is shown, etc., etc..


  • When running inside X, the X clipboard buffer is used for copy and paste operations.


  • Syntax highlighting, including coloring many types of errors in red.


  • Good error reporting, including actually helpful error messages, stack traces with line numbers and argument lists, and detection of common errors with suggestions on how to fix them.


  • 'Universal variables', i.e. variables whose value is shared between all the users fish instances, and which are automatically saved across reboots. This is a very nice way to change configuration options like PAGER without having to edit any files.


  • No configuration required, fish comes with a sane default configuration, to use zsh at it's best you have to spend a lot of time tweaking your<nobr> <wbr></nobr>.zshrc file.


  • A much improved syntax. No more strange language quirks like whitespace sensitivity.

#

bash_completion

Posted by: Administrator on May 17, 2006 12:12 AM
If you don't have the mentioned (excellent) bash_completion file on your system (it's not a standard part of bash, it's an add-on), then check if your distro offers a package for bash_completion or bashcompletion. If not, you can download it and install it yourself.

<a href="http://freshmeat.net/projects/bashcompletion/" title="freshmeat.net">http://freshmeat.net/projects/bashcompletion/</a freshmeat.net>

I don't use any of these ESC key combinations because bash_completion does very smart completions for most of the commands that I use. I wrote one or two of my own completions for proprietary tools that we use at work. Check the bash man page for the "complete" command to see how to customize completions yourself.

#

Corrections

Posted by: Administrator on May 10, 2006 01:14 AM
"As root, edit the<nobr> <wbr></nobr>/etc/bash.bashrc file"
<nobr> <wbr></nobr>/etc/bash.bashrc is not a standard file, and will not be found on many distros.

The bash_completion script may not be in<nobr> <wbr></nobr>/etc; it may be in<nobr> <wbr></nobr>/etc/profile.d, it may be broken into several files, or it may be absent altogether.


"The caret wildcard excludes files. mv *[^.php]<nobr> <wbr></nobr>../ would move all files to the parent directory, exculding those with<nobr> <wbr></nobr>.php extension."



This is incorrect; mv *[^.php]<nobr> <wbr></nobr>../ will move all files which do not end in '.', 'p', or 'h'. The second 'p' is redundant. The square brackets contain a list of characters that will match (or not match if ^ is used) a single character, not a string.

#

This story has been archived. Comments can no longer be posted.



 
Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya