22

I am on Debian Jessie 8.6. I noticed that apt-get gets the expected bash autocompletion when pressing tab for packages and command, but when trying to use it with apt it does not work. I remember using xubuntu 16.04 where it worked, so I find it strange that it does not work here. Is there a way to enable it for the command apt as well? If so, how?

  • Did anything work for you? – Lynob Jul 25 '19 at 20:38
  • @Lynob I ended up moving to Ubuntu, so I can't help you, sorry. – Andrea Jul 27 '19 at 13:02
21

Debian does not come with 'bash-completion' installed and enabled.

If you're coming to Debian from, say, an Ubuntu background, where it is pre-installed and enabled by default, this can be a source of some confusion.

To enable fix this, run (as root):

apt-get install bash-completion

Then, you have two options. You can either:

 1. Enable it on a per-user basis for yourself, or

 2. Enable it globally.

1. If you want to enable it for just your user, edit ~/.bashrc - add the following:

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

To try it without logging out and back in, run:

    . ~/.bashrc

Or open a new shell. Then try to use tab-completion with apt. That dot and space at the beginning (.) is the same as using the source keyword in bash, but is more portable.

If you want it to work when su'd into the root account, do the same thing in root's home directory (typically /root).

2. To enable it globally, do the changes from (1) in the file /etc/bash.bashrc instead.

To anyone who's wondering why this works, the . in front of /etc/bash_completion does not refer to the current directory, since it has spaces around it. It instead makes the contents of the given file be evaluated in the currently running shell, instead of being executed in a new subshell. It is standardized here.

In Bash, this . can be replaced by the command source, but this is not standardized by POSIX and is less portable so I tend to steer people away from using it. In this case, since it is specifically a program for extending bash, rather than something that needs to work in a bourne shell or ksh, you can feel free to substitute source for readability.

Incidentally, this behavior (not opening a sub-shell) is similar to the way DOS/Windows .BAT scripts work normally, changing the state of the shell they are run in. This is why if you cd into a different path in a shell script, you won't be in that path when the script exits like you would be in a .BAT.

| improve this answer | |
  • 2
    I already did these steps, but that didn't help. Only works with apt-get, but apt doesn't. – Andrea Sep 26 '16 at 15:36
  • @Andrea Oh, sorry, I didn't fully read your question, I guess. I'm not at my Debian box at the moment (in windows, all I've got is cygwin, unfortunately), but I'll try to figure it out from my debian machine when I'm back home. – Wyatt8740 Sep 26 '16 at 19:20
6

In Debian 9 Stretch just edit /etc/bash.bashrc and uncomment:

# 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
| improve this answer | |
3

Copy this configuration file from Ubuntu: /usr/share/bash-completion/completions/apt to the same directory in Jessie. You can also get the file here: http://pastebin.com/PRBMt3an

| improve this answer | |
  • This is what I need. But auto completion is not working for apt, after I copied, renamed the downloaded apt file to the /usr/share/bash-completion/completions/. How to make it work? – klor Jan 27 '19 at 14:38
  • The pastebin.com/PRBMt3an file was in Windows CR LF linefeed, this is why it did not work. I had to convert line format to Unix LF linefeed, so then worked after starting a new terminal. – klor Mar 17 '19 at 13:56
  • Here is the 'apt' file with correct linefeed: filedropper.com/apt_1 – klor Mar 17 '19 at 14:08
  • This will work, but it's basically just implementing what I do in my answer. If you do it this way instead, know that you can use dos2unix to convert the line endings on files from pastebin (you might have to install it, though). You can also use this command, but to get the ^M Carriage Return character you'll need to do ctrl-v ctrl-m; the 'carat-capital-M' won't actually work as is off the clipboard: sed 's/^M$//' < file-to-fix > fixed-file. – Wyatt8740 Apr 28 at 2:02
1

How I solved it

I had auto completion problems trying to autocomplete wget with apt-get install wg[TAB][TAB] and I managed to solve it by removing the --no-generate parameter from the flies below:

/usr/share/bash-completion/completions/apt-cache
/usr/share/bash-completion/completions/apt-get

It is worth mentioning that these files are available only after you install bash-completion.

The --no-generate parameter I'm referring to can be seen in the line below, but I removed ALL --no-generate, not only from this line: https://github.com/scop/bash-completion/blob/master/completions/apt-get#L28

How did I come to this point?

I got to this point after running set -v previous to apt-get install wg[TAB][TAB], which showed that the command executed was:

apt-cache --no-generate pkgnames wg

and the output was an error:

E: Could not open file  - open (2: No such file or directory)

When I executed apt-cache pkgnames wg without the --no-generate I correctly got the list for autocompletion:

root@484f5c8f56ba:/# apt-cache pkgnames wg
wgalician-minimos
wget2
wgaelic
wget
wgerman-medical
wget2-dev

Hope this helps.

| improve this answer | |
  • That was exactly what I needed. Thanks :D – Ceisc Jan 10 at 22:35
0

The problem is most probably caused by

Dir::Cache::pkgcache "";
Dir::Cache::srcpkgcache "";

settings in /etc/apt/apt.conf.d. In my case, it originated from Debian Docker image, see https://github.com/debuerreotype/debuerreotype/blob/799473bee3d388cf00bd55221b38a9d58bd6a2ba/scripts/debuerreotype-minimizing-config

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.