bash-completion

Jon Shea May 20th

Are you using bash-completion? I bet you think you are. I bet you’re not. It’s a horrible name (up there with “The Wire” and “Battlestar Galactica”), because people hear it and think that they know what it is, but they’re wrong. Don’t prejudge the bash-completion. It’s much, much more.

Everyone knows about [tab] completion. It’s great. We love it. But, it leaves a lot to be desired. How many times have you done something like this:

Continuity:~$ cd m[tab]

and then seen this:

magnetk/     makay_tex_source.tex     mathjob.pdf     
methods of theoretical physics.pdf     monterpp.pdf

Three pdfs, a tex source file, and one directory. Do you know how many times I’ve wanted to cd into a pdf file? Zero. I’ve never wanted to cd anything but a directory. So why does [tab] completion show me all those files that I’m obviously not interested in?

There’s a better way. In fact, bash already has a robust programmable completion system managed through the builtin commands compgen and complete. While bash is distributed with “programmable completion”, it isn’t distributed with “programmed completion”.

That’s where bash-completion comes in. It’s more than 9000 lines of pre-programmed completion artificial intelligence. It knows that you only cd into directories. It knows passwd only works on users, groupmod only works on groups, unalias only works aliases, and which only works on commands. And that’s just in the first 150 lines. The current version even includes a whole svn subprogram, so that it can figure out valid targets even for the various svn commands.

You can install bash-completion by Macports (sudo port install bash-completion), fink (fink install bash-completion), or apt-get (apt-get install bash-completion). You can also install it by hand. This involves downloading the file, unzipping it, and putting it somewhere.

Regardless of how you install it, you need to make sure it gets sourced when you start a new shell. Sometimes Macports et alias will do this for you, and sometimes they won’t. If you’re new to this, then source just reads a file and executes it line by line. Add a line to .profile (or another file that gets loaded when you open a new shell) that says source /path/to/bash_completion. For those of you with Twitter-eqsue character constraints on your dot-config files, you can also say . /path/to/bash_completion, where the . operator is a synonym for source that saves 5 characters at the cost of being infinitely less readable, and infinitely easier to misinterpret.

Then you’re done. Load up a new shell and enjoy. If you’re used to stupid file completion, then prepare to be amazed. You’ll find yourself groping around with [tab] in places you never would have imagined before, and you won’t even realize you’re doing it.

Tags:

5 Responses to “bash-completion”

  1. Kemal Hadimli Says:

    awesome!

  2. moltar Says:

    Or you can switch to zsh

  3. moltar Says:

    Here is the tut btw with a nice profile file with colors and all:

    http://friedcpu.wordpress.com/2007/07/24/zsh-the-last-shell-youll-ever-need/

  4. Jon Shea Says:

    Yeah, yeah, yeah, zsh users always say that when you bring up bash-completion, but it doesn’t really hold water. I’m not a zsh user, but my understanding is that the zsh programmable completion system is distributed un-programmed, just like the bash programmable completion system.

    Zsh might be a whole new world. It could be the best thing humanity has ever seen—I don’t really know—but it isn’t any better at completion than bash is.

  5. huh? Says:

    you r not a zsh user yet , thats why u saying. Zsh have WAY COOLER stuff loaded inside. I have been bash user for 5 years , almost nothing in functionality have been improved , until i met zsh a few week agos and now i fall in love . The Whole concept of shell is revoultionized ! Heck yeah , you can press ALT+H in the way of writing a multipipe command just to bring up man page , then quit man , you can continue stuff. The Completion system ins menu based which is so l33t .

    Live on the edge , use Zsh! or you can do all same old shit for year everyday.

Leave a Reply