Programmable bash completions in Ubuntu

Alex mentioned to me last week that there is a feature in bash which lets you do "programmable completions". By default, type a few letters in bash, then press tab, and bash will attempt to work out the command you are trying to run. For example, if you type "syna", then tab, bash will expand it to "synaptic". If there are multiple possible completions, bash will show a list.

This is a great feature. But Alex said it goes further than this, so that you can actually get further completions once you've typed the name of the script/binary you want to run. To enable this, under Ubuntu you uncomment these lines in ~/.bashrc:

if [ -f /etc/bash_completion ]; then
    . /etc/bash_completion
fi

This means you can now do things like type "svn", then press tab, and get a list of the possible arguments you can pass to the svn command ("list", "checkout", "rm" etc.).

Or you can type "ssh", then tab, and get a list of the hosts in your SSH known_hosts file.

Or type "apt-get", then tab, and get a list of the apt-get subcommands.

Presumably you can add your own if you are so inclined. Very handy. Thanks Alex!

Comments

I like your article. Love

I like your article. Love your it support

interesting

Very helpful tip, I've never programmed in Ubuntu, only Linux and MS, so that should be a first for me. I'm thinking of putting together some programming lessons, in the style of my guide on port forwarding, but who knows?