60

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?

Screatch
  • 619
  • I solved this problem using the solution given at http://askubuntu.com/questions/325807/arrow-keys-tab-complete-not-working – D-B Dec 18 '13 at 22:24

5 Answers5

78

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
Adam Byrtek
  • 9,811
  • Thanks!

    I have replaced .bashrc with my own and therefore could not figure out what the problem was in. Works perfectly now.

    – Screatch Apr 04 '11 at 11:13
  • this fixed my git bash autocompletes at xubuntu 12.04 too, many thanks. – palmic May 27 '12 at 22:40
  • How about auto-complete for switch and option of commands line apt-get? i.e when you enter apt-get ins and then press the tab to auto-complete it. – Reza Ghodsi Nov 18 '16 at 10:56
  • 4
    I'd like to add that the package bash-completion is necessary for this to work. Weirdly, the standard installation of my company doesn't have this package. – Edgar May 17 '17 at 07:15
  • Where should .bashrc be located? – Gulzar Nov 02 '21 at 11:55
  • it depends upon system. usually you can find it within your user's home directory, but it is hidden. All .* files are hidden. Use ls -lha to see it. '-a' does the trick . Additionally to what Adam said i would add to fire "exec bash" to reload the bash with newly edited settings (of course, the option of logging off and on remains too for the same purpose.) – George Mar 21 '24 at 08:24
16

In addition to modifying /etc/bash.bashrc file (se other answers) you may need to install bash-completion package.

Check, if /etc/bash_completion file exsits. If no:

sudo apt-get install bash-completion

(In my intallation of Debian 7 it was not initially installed)

Serge S.
  • 261
  • 2
  • 4
14

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.

  • 1
    Nice explanation, it solved my problem when I am trying tab completion and error occured >> words[0]=${!ref}${COMP_WORDS[i]} – sugab Jun 02 '15 at 00:55
  • While that works as a workaround, it's a poor thing for any completion programmer to rely on. Since nullglob (which is nominally better to many) and kin might be set, any completer code which requires the old semantics (wildcards without matches result in the wildcard itself) should save the current glob setting, set the one they need, and restore the old glob setting afterwards, just as IFS is typically used. – Alex North-Keys Aug 05 '15 at 14:57
9

As basic as it may sound make sure that bash is your active shell

echo $SHELL

I just upgraded to Ubuntu 14.04.1 LTS and had no tab auto-completion, even after following the advice on this post, only to realize my shell was set to /bin/sh and not /bin/bash.

ph0t0nix
  • 1,377
nocdib
  • 91
4

Updating to Ubuntu 14.04.01

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

sudo vim /etc/bash.bashrc (type "i" to edit the file)

    # enable bash completion in interactive shells
    if ! shopt -oq posix; then
     if [ -f /usr/share/bash-completion/bash_completion ]; then
        . /usr/share/bash-completion/bash_completion
      elif [ -f /etc/bash_completion ]; then
        . /etc/bash_completion
      fi
    fi

Uncomment 7 lines above

Save file "esc + w + q + enter"

Restart bash

exec bash