15

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 at 20:38
  • @Lynob I ended up moving to Ubuntu, so I can't help you, sorry. – Andrea Jul 27 at 13:02
17

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

To 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.

  • 1
    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
4

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
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

  • 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 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 at 13:56
  • Here is the 'apt' file with correct linefeed: filedropper.com/apt_1 – klor Mar 17 at 14:08
0

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.

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.