bash-completion: the greatest things since bash completion
March 1st, 2009 edited by TinchoArticle submitted by Andre Masella. Guess what? We still need you to submit good articles about software you like!
Pressing the tab key in bash to auto-complete a file name is one of the most time saving tricks especially when dealing with very long file names. Unfortunately, file name completion is not always the right behavior. Take Subversion for example. The first argument to svn
is the sub-command to use. The file name is also restricted: svn add
only takes files not under revision control and svn rm
only takes files that are under revision control.
This is where the bash-completion
package steps in. After installing it with a quick apt-get install bash-completion
, a few lines need to be uncommented in sudo vim /etc/bash.bashrc
and the shell restarted. After that, try this:
$ svn <TAB><TAB> add cl diff list move propdel rename unlock annotate cleanup export lock mv propedit resolve update blame co -h log pdel propget resolved --version cat commit help ls pedit proplist revert changelist copy --help merge pget propset rm checkout cp import mergeinfo plist pset status ci delete info mkdir praise remove switch
Ta-da! Smarter completion for subversion.
bash-completion
will alter the behavior of most commands to limit the display to relevant files. For example, mpg321
will only display MP3 files in the list. Programs like rmmod
, iwconfig
, ifup
, and lvm
will display relevant choices that are not files at all. Even bash’s fg
and bg
will now tab-complete with job identifiers.Completion for man
is useful as it will auto-complete only man pages that exist and allows you to incrementally narrow your search by providing the beginning of the man page name, just like with regular files.
Occasionally, it doesn’t behave as expected. Particularly, sometimes a file with the wrong
extension will be filtered out by bash-completion. For example, if you save an image for certain Internet forums, the file will sometime lack an extension. bash-completion will then filter out that file because it does not have the right extension. This can also happen if the capitalization is unusual. For example, bash-completion will suggest files that end in .mp3 or .MP3 for mpg321, but not .Mp3. In that case, either rename the file or insert #
at the beginning of the line. The #
makes bash think this line is a comment and bash-completion returns to regular file name completion. Once finished, remove the #
and run the command.
bash-completion
is available in Debian and Ubuntu. If it isn’t available, it is very easy to install from source.
March 1st, 2009 at 6:09 am
Hrm, I’ve had the bash-completion package installed for a long while. I noticed after reading this article that it _isn’t_ enabled till you edit /etc/bash.bashrc.
March 1st, 2009 at 12:00 pm
I didn’t know this, that is one of the reasons I installed Zsh. On the other hand, it’s not the only reason :P
March 1st, 2009 at 12:30 pm
It also reads SSH configuration (~/.ssh/config) and parses the hosts defined there (even host aliases defined like “Host http://www.example.com example”, a feature which is undocumented in ssh_config man page, to my knowledge)
Also useful: when using cdargs bookmarks utility, you can not only jump to your bookmarks via “cv” alias, but you can also descend into the directories behind them via bash tab-completion.
Sebastian
March 1st, 2009 at 5:23 pm
yes ist is better than nothing but for me as regular shell-user its too often annoying… why not show all files if tab is pressed once more? nothing.. the tip with the # is not cool!
i switched to zsh two days ago. this is also very annoying, but its getting better, since everything is configurable (see compinstall). maybe i’ll stay with zsh.
but for bash this is a must-have compared to the buit-in completion
March 1st, 2009 at 6:33 pm
Instead of the # trick, I use alt-/. That will always do normal completion instead of the programmable completion.
March 1st, 2009 at 8:45 pm
I wish there was a key combo, say shift-tab that reverted to ye olde dumb, but sometimes more useful,current working directory files filename completion.
eg. When I’m using cvs, most cvs modules that I wish to refer to are already in my cwd, and I explicitly _don’t_ wish to refer to any other.
Alas, smart completion goes off and checks out CVSROOT/modules and presents a LARGE list I really don’t want.
March 1st, 2009 at 8:49 pm
Ok, so I didn’t read all 5 comments. I should have. Thanks Erik for preanswering my question. At least you now have the surety that your comment is appreciated!
March 2nd, 2009 at 10:46 pm
One thing to note, though, is that enabling bash-completion slows down the startup time of bash. If you’re like me and open new terminals willy-nilly (Ctrl+Alt+T is reason enough to madly love GNOME Terminal: instant new shell in the right $PWD) this becomes very noticeable. I once took the time to extract the completions I liked most (like remote filename completion in scp command lines, awesome if you use ssh-agent) into a cut-down version sourced from my .bashrc.
Nice tricks with the #, and Alt-/
March 4th, 2009 at 8:21 am
typo: mpg321
also bash-completion will show directories as well for mpg123.
March 4th, 2009 at 2:58 pm
Are there docs somewhere on how to make your own program supply auto-complete information to bash? I’m just wondering how it figures out “available” items.
March 4th, 2009 at 8:32 pm
James:
You can learn more about how bash-completion works in a very nice Debian Administration article: part 1 and part 2. If you want more detail, you can always check the bash manpage.
March 6th, 2009 at 7:34 pm
Hot damn, thanks for that Alt-/ tip! Bash completion is nice, but I know it has infuriated me in the past when the filename extension filter got too zealous.
March 18th, 2009 at 6:06 am
The following ~/.inputrc tweaks are indispensable (though I’m not sure why their equivalents don’t seem to work in ~/.bashrc):
# tired of having to mash tab twice?
set show-all-if-ambiguous on
# no “are you sure you want to see this many completions?” prompt
set completion-query-items 10000
# more is the most annoying program on the planet
set page-completions off
March 18th, 2009 at 12:06 pm
You may also want to turn on inline mode for tab completion:
“\t”: menu-complete
http://codesnippets.joyent.com/posts/show/1690
April 2nd, 2009 at 11:06 am
When will linux distros get rid of the obsolete bash to replace it with the all-mighty zsh ?
June 5th, 2009 at 4:51 pm
> a few lines need to be uncommented in sudo vim /etc/bash.bashrc
Aaah, which lines would those be? My bash.bashrc doesn’t have any lines there that mention this package/functionality.
June 6th, 2009 at 6:41 am
$ grep -C1 completion .bashrc
# enable programmable completion features (you don’t need to enable
# this, if it’s already enabled in /etc/bash.bashrc and /etc/profile
# sources /etc/bash.bashrc).
if [ -f /etc/bash_completion ]; then
. /etc/bash_completion
fi
June 6th, 2009 at 6:43 am
$ grep -C1 completion /etc/bash.bashrc
# enable bash completion in interactive shells
#if [ -f /etc/bash_completion ]; then
# . /etc/bash_completion
#fi