POW: bash-completion – Bash Auto Completion in Fedora using yum (and more)

In an attempt to consistently blog, I am starting a new series here on fedora-tutorials.com. Program of the Week (POW). Hopefully, this will excite and inform all of us about the cool programs available in Fedora. So see you next Wednesday for another program.

Over the past year or so, I’ve been on the hunt for several things that I find in other Linux distros that I cannot find in Fedora. Its not very common, but on occasion I do run into something that’s not there on Fedora. One of them was the ability to automagically complete many of the command lines for many things.

One of them, and probably one of the biggest, was the fact that yum did not have tab completion for available packages. Today is my lucky day! While chatting and helping my students with their labs today, one of them mentioned to me that he could tab complete a particular command on his box. I of course inquired, because it interested me, as to the package name. It turns out my bash tab completion dreams were just about to come true! He told me about this amazing package that would let me use tab completions for things like the service command, man and of course, yum.

I was blown away! So immediately after this discussion, I started searching for this elusive package I’ve never heard of before. Sure enough, as he informed me, bash-completion does exist and does some amazing things. After hunting around a little on google, here’s some of the stuff I found. I’ll also include the links at the bottom of this post.

as root try this: (note [Tab] means you should hit the tab key)

# service ht[Tab]

What you’ll notice is that one of three things happen. If you’ve got the bash-completion package installed already because you’re ahead of the game, it should auto-complete for you. Without bash-completion, this doesn’t happen, but its also possible that since bash already has some completion in place, it might auto complete a directory for you, but that’s definitely not what you want.

If you’ve not already installed bash-completion, I’d suggest you do it now. On Fedora 7, run the following command:

# yum install -y bash-completion
..snip..
Installed: bash-completion.noarch 0:20060301-3.fc7
Complete!

Now that bash-completion is installed, we need to invoke the tools. Normally, this is not needed, and a reboot/re-login will take care of this as well, but since I wanted to use this right away, I did the following as an unprivileged user:

$ source /etc/bash_completion

This doesn’t seem to do much, but its actually quite powerful. The source (or .) will load the environment variables from the /etc/bash_completion script into my current environment. Luckily for us, when we now log into root, /etc/bashrc will accomplish this for us without any intervention. To test that it worked, try running the following command as the same unprivileged user:

$ unalias[Tab]
.=     ll=     ls=     vi=     which=

Note that when I pressed twice, a list of the currently available aliases appeared. Nice ey? Let’s complete this:

$ unalias w[Tab]

Now produces:

$ unalias which

And completes the string as expected. Now we’re getting somewhere! But why did I really want to explain this?

Oh yeah! yum

With bash-completion, yum can now provide us with a list of available packages, similar to the auto completion capability in apt-get or aptitude from Ubuntu or Debian. Say for instance you want to see all of the packages available for install that match what you’re looking for, but don’t want to run yum list or yum search because, in truth, it just takes to long! Now you have an alternative:

# yum -y install bal[Tab]

Produces:

ballbuster.i386  ballz.i386       balsa.i386

Adding another ‘lb’ to the end of that string (and then the tab key of course) should help us to complete to the package we’d like to install.

# yum -y install  ballb[Tab]

Then completes to:

# yum -y install  ballbuster.i386

Hitting enter then installs the ballbuster package, and its quite a fun game!

.. snip ..
Installed: ballbuster.i386 0:1.0-1.fc6
Dependency Installed: ClanLib.i386 0:0.8.0-4.fc7
Complete!

Of course, there are hundreds of others tab completions available (and there’s a good way to list many of them too, even if its a bit cryptic). Try these on for size:

Are you a developer?

$ svn c[Tab]
cat checkout  ci     cleanup   co     commit    copy    cp
$ make [Tab]
all clean dist-clean

What about a systems administrator?

# modprobe -r b[Tab]
battery    bay        blkcipher  bluetooth  bridge     button
$ man cron[Tab]
cron     crond    crontab
$ ssh herlo[Tab]
herlo-f7   herlo-lap  herlo.org
$ grep --[Tab][Tab]
 --after-context=  --directories=   --invert-match   --only-matching
 --basic-regexp    --exclude=       --label=         --perl-regexp
.. snip ..

To help you wade further through, try out the following two commands:

  • complete -p
  • declare -f

Be aware that these are advanced components and can really be confusing if you’re not a developer and just want to use the features. The complete command seems to provides some tools to do additional auto-completion. I also think that its nice to be able to extend this functionality to other applications as well.

As promised, here’s a few links to help your completion introduction. Note: Some of these links provide more than just the simple tab completion:

This entry was posted in Bash, Fedora, POW, Tech, Tools, Yum and tagged , , , . Bookmark the permalink.

7 Responses to POW: bash-completion – Bash Auto Completion in Fedora using yum (and more)

  1. Chris says:

    Fantastic!

  2. Great package and tip. Always wanted something like this but I bothered to go hunting for it..

    Thanks again.

  3. Peter says:

    Outstanding tool.
    Thanks for sharing.

    N.B. Came across another ‘helpful’ utility, if you need an inventory of your current hardware and its settings http://ezix.org/project/wiki/HardwareLiSter
    lshw or gui driven lshw-gui use as root…

  4. Derek says:

    While this is an awesome utility, it takes too long for it to list installable packages in the yum repositories. For example, “sudo yum install kernel-[tab]” takes a long time before it displays a list of possible packages.

    Is there any way to speed this up?

  5. herlo says:

    Derek,

    I’m considering submitting a solution that would read the yum cache as opposed to using the repos themselves. bash_completion can be scripted to accomplish this but I’m not completely up-to-date on it…

    Cheers,

    Herlo

  6. Randell says:

    One of the problems I encounter with bash-completion is that it appends an extract space when tab-tabbing paths instead of appending a slash for directories.

  7. herlo says:

    Randell,

    Yeah, I hear you there. Something has changed recently in the way they handle that, I noticed it too. It seems only in the latest version, maybe rolling back until they fix whatever is messed up?

    Clint

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>