76

At some moment, the apt-get install autocomplete was broken. It was very useful to me to see the names of packages available for installation when pressing Tab.

For example, if I would press the Tab key after sudo apt-get install ruby, all packages starting with ruby would be displayed, such as ruby-mysql, ryby-dev, etc.

Instead of this behavior, if I press Tab, I get a list of directories, as I get when I use cd and press Tab.

How to repair this autocomplete?

1
  • What package you were trying to find? Is it available via apt-cache search mypkg I am using Kubuntu on Oneiric i see it working well.
    – koolhead17
    Dec 9, 2011 at 11:18

4 Answers 4

118

As stated in the comments to the other answer, first make sure that bash-completion is installed:

sudo apt-get install bash-completion

and apparently for @diapir, it helped to reinstall it:

sudo apt-get install --reinstall bash-completion

However, for me, the problem was not that bash-completion was not installed or broken, the problem was that I had accidently deleted my .bashrc file. You can get a new one by copying it from /etc/skel:

cp /etc/skel/.bashrc ~/
4
  • 2
    Thanks, the bashrc tip solved the problem for me as well. The use of sudo on the chmod command is unnecessary though, right? Dec 24, 2012 at 4:32
  • 2
    the file located in /etc/ is 0644/-rw-r--r-- with user:group = root:root, and when copied to your user folder, will automatically go to user:user
    – Kevin
    May 28, 2014 at 17:07
  • cp /etc/skel/.bashrc ~/ helped me solve this problem. Nov 15, 2015 at 8:54
  • Reinstall and copied /etc/skel/.bashrc helped me, thank you!
    – Sergey
    Apr 27, 2016 at 10:19
38

Type the following command:

sudo -H gedit /etc/bash.bashrc

Look for these lines:

# 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 some lines, to make it look like this:

# 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

Save, close terminal, then reopen it.

I have this issue after installing 13.04, and that's how I resolve it.

2
  • this was the only thing that worked for me. thank you.
    – this.foo
    Nov 27, 2015 at 7:40
  • Worked for me on Elementary OS LoKi Apr 5, 2017 at 14:43
12

Note that output of this command:

apt-config dump | grep "Dir::Cache"

Should be something like this:

Dir::Cache "var/cache/apt";
Dir::Cache::archives "archives/";
Dir::Cache::srcpkgcache "srcpkgcache.bin";
Dir::Cache::pkgcache "pkgcache.bin";

If value of Dir::Cache::pkgcache is empty, apt will not be able to auto complete package names. Take a look at your /etc/apt/apt.conf.d/ files and search for this key to be sure that it's not set to empty string.

grep -r pkgcache /etc/apt/apt.conf.d/

To fix it comment out all lines of /etc/apt/apt.conf.d/docker-clean

# DPkg::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };
# APT::Update::Post-Invoke { "rm -f /var/cache/apt/archives/*.deb /var/cache/apt/archives/partial/*.deb /var/cache/apt/*.bin || true"; };
# Dir::Cache::pkgcache ""; Dir::Cache::srcpkgcache "";

and then

apt update

Also take a look at this answer.

2
  • 3
    Looks like this (and the link) is the correct answer if you have this issue inside a docker container.
    – Alan Wang
    Feb 24, 2020 at 14:00
  • this still works tysm! just do rm /etc/apt/apt.conf.d/docker-clean
    – Dave Ankin
    Feb 14, 2021 at 15:45
4

Download the latest stable release of bash-completion from http://bash-completion.alioth.debian.org/, and from within the downloaded directory use the following commands to build and install it:

./configure
make
sudo make install

If you don't have make installed, you can install it with the build-essential package:

sudo apt install build-essential

After successful installation, insert the following lines to your ~/.bashrc file:

if [ -f /etc/bash_completion ]; then
  . /etc/bash_completion
fi
4
  • bash-completion is in packages - is there any reason you did it manually? The package should do pretty much exactly this :)
    – Caesium
    Dec 10, 2011 at 12:46
  • :) as always, sitting, reading manuals, configure, and then it turns out that this soft is exists in the repository Dec 10, 2011 at 12:57
  • 4
    sudo apt-get install --reinstall bash-completion did it for me.
    – diapir
    Apr 30, 2012 at 0:48
  • 2
    @diapir, I have the same problem, but only reinstall does not working. I have to apply the lines in bashrc file.
    – Anwar
    May 13, 2012 at 17:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .