Tab completion doesn't work for commands

I am trying to use tab completion for commands in Ubuntu 11.04 Beta 1, running all the latest updates, but it doesn't seem to work. (Though package bash-completion is installed)

Tab completion works correctly only for directories or files.

Any ideas where to dig?


Solution 1:

Check whether you have the following fragment in your .bashrc (should be there by default)

# 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 ] && ! shopt -oq posix; then
    . /etc/bash_completion
fi

Solution 2:

I thought I would also add the following additional explanation for a failure of bash completion- it is too long for a comment and may be useful for someone who has a problem with bash completion, as presumably this is the 'general' canonical question to which all duplicates are referred.

Even if you have the appropriate line in your .bashrc (as in the answer above) and the bash-completion package installed, bash completion can still malfunction if you have either shopt -s nullglob or shopt -s failglob in your .bashrc or .bash_aliases.

The following scenarios were tested with all user customisations of .bashrc and .bash_aliases removed to give a fair test, with the appropriate bash completion line in .bashrc present and with bash-completion installed.

1) The first scenario is when you have shopt -s failglob in your .bashrc or .bash_aliases.

When trying to tab complete directories and files respectively, these errors will occur:

cd Dbash: no match: "${COMP_WORDS[@]}"  
cat pabash: no match: words[0]=${!ref}${COMP_WORDS[i]}

However, commands will still complete successfully.

2) With shopt -s nullglob enabled, trying to tab complete a directory or a file will instead lead to everything being listed, with no completion:

cd Do
.adobe/                  .gimp-2.6/               Podcasts/
.aptitude/               .gnome2/                 Public/
.audacity-data/          .gnome2_private/         .pulse/
............

I have cut short the listing here, as it was very long and not necessary to reproduce it all.

Again, command completion will still work however.

So, just removing shopt -s failglob or shopt -s nullglob could also solve the problem of errors with bash completion, assuming that you have the appropiate line in your .bashrc and bash-completion installed.