Can I enable bash-completion for the new apt command?
Solution 1
This is an omission in the bash-complete
package, not apt
. It just seems a completion doesn't exist yet, so I've scrapped together what I can for the apt
command (it's not the best documented command that's ever existed!)
The following is an adaptation from the existing apt-get
completion (with elements stripped out and bits added from apt-cache
's completion). Run sudoedit /usr/share/bash-completion/completions/apt
and paste in the following:
# Debian apt(8) completion -*- shell-script -*-
_apt()
{
local cur prev words cword
_init_completion || return
local special i
for (( i=0; i < ${#words[@]}-1; i++ )); do
if [[ ${words[i]} == @(list|search|show|update|install|remove|upgrade|full-upgrade|edit-sources|dist-upgrade|purge) ]]; then
special=${words[i]}
fi
done
if [[ -n $special ]]; then
case $special in
remove|purge)
if [[ -f /etc/debian_version ]]; then
# Debian system
COMPREPLY=( $( \
_xfunc dpkg _comp_dpkg_installed_packages $cur ) )
else
# assume RPM based
_xfunc rpm _rpm_installed_packages
fi
return 0
;;
*)
COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
2> /dev/null ) )
return 0
;;
esac
fi
case $prev in
-c|--config-file)
_filedir
return 0
;;
-t|--target-release|--default-release)
COMPREPLY=( $( apt-cache policy | \
command grep "release.o=Debian,a=$cur" | \
sed -e "s/.*a=\(\w*\).*/\1/" | uniq 2> /dev/null) )
return 0
;;
esac
if [[ "$cur" == -* ]]; then
COMPREPLY=( $( compgen -W '-d -f -h -v -m -q -s -y -u -t -b -c -o
--download-only --fix-broken --help --version --ignore-missing
--fix-missing --no-download --quiet --simulate --just-print
--dry-run --recon --no-act --yes --assume-yes --show-upgraded
--only-source --compile --build --ignore-hold --target-release
--no-upgrade --force-yes --print-uris --purge --reinstall
--list-cleanup --default-release --trivial-only --no-remove
--diff-only --no-install-recommends --tar-only --config-file
--option --auto-remove' -- "$cur" ) )
else
COMPREPLY=( $( compgen -W 'list search show update install
remove upgrade full-upgrade edit-sources dist-upgrade
purge' -- "$cur" ) )
fi
return 0
} &&
complete -F _apt apt
# ex: ts=4 sw=4 et filetype=sh
Then run source ~/.bashrc
to load the completion. Then apt show firef
+ Tab should complete.
This may offer you options that just don't exist any more. I think I've nailed the main commands (which might change in time) but at the very least it'll help you with the common commands: list
search
show
update
install
remove
upgrade
full-upgrade
edit-sources
dist-upgrade
purge
.
Obviously, if a bash-completion maintainer wants to nab the above, you're welcome to it under GPL (though I'd be tempted to start from fresh once apt
is documented!)
Solution 2
Why not use the original bash-completion?
Try this script. It will download and install the bash-completion on ~/tmp/bash-completion
.
#!/bin/bash
echo -en "\e]2;Updating bash completion...\a"
katalog="~/tmp/bash-completion"
if [ ! -d "$katalog" ]; then
mkdir -p $katalog
cd $katalog
cd ..
git clone git://git.debian.org/git/bash-completion/bash-completion.git
cd $katalog
autoreconf -i
./configure
make
sudo make install
else
cd $katalog
if [ `git log --pretty=%H ...refs/heads/master^` != `git ls-remote origin -h refs/heads/master |cut -f1` ]; then
git pull
autoreconf -i
./configure
make
sudo make install
else
echo "Bash-completion is already up to date!"
fi
fi
You start using it with command . ~/tmp/bash-completion/bash_completion.sh
, which can be put into ~/.bashrc
file, or - better yet - symlink it into some file in the /etc/profile.d/
directory. Uninstall the original bash-completion, so you wouldn't end up loading both at the same time.
Related videos on Youtube
Oli
Hi, I'm Oli and I'm a "full-stack" web-dev-op. Eurgh. I'm also allergic to jargon BS. I spend most of my professional time writing Django websites and webapps for SMEs. I write a lot of Python outside of Django sites too. I administer various Linux servers for various tasks. I contribute to the open source projects that I use when I can. I'm a full-time Linux user and that has lead to helping other people live the dream. I am an official Ubuntu Member and I earnt my ♦ on SE's own Ask Ubuntu in 2011's moderator election. That's probably where I spend most of my unpaid time. I also run thepcspy.com which has been my place to write for the last decade or so. If you need to contact me for extended help, you can do so via my website, just remember that I have bills so if I feel your request is above and beyond normal duty, I might ask for remuneration for one-on-one support. For more social contact, you can usually find me (or just my computer) lurking in the Ask Ubuntu General Chat Room and on Freenode in #ubuntu and #ubuntu-uk under the handle Oli or Oli``.
Updated on September 18, 2022Comments
-
Oli almost 2 years
The new
apt
command, present in Ubuntu since 14.04, seems to be a really useful intersection of functionality betweenapt-get
andapt-cache
but the current version ofbash-completion
doesn't know about it... Which makes it a lot harder to use.Is there a quick way to add this functionality to Bash to make the
apt
command easy to use?-
h3. about 10 yearsThe tagging of this question is begin discussed on meta.
-
-
Jorge Castro about 10 yearsOpen a bug and submit this as a patch!
-
Oli about 10 yearsI hadn't thought of this but they also don't seem to have an apt completion yet
-
Adam Ryczkowski about 10 years@Oli Well, I guess they do. There are files
aptitude
,apt-get
andapt-cache
. What exactly do you mean by apt completion? -
Oli about 10 yearsPer my opening question,
apt
is a brand new(ish) command that's in Trusty. It has some of apt-get, some of apt-cache... All with a little bit of extra flourish in one place. -
Adam Ryczkowski about 10 years@Oli Oh, you are right! I didn't know about it. And - bash-completion doesn't support it out of the box so far. Fortunately it wouldn't be that difficult to write a plugin, so you can expect one soon. Chances are, that using my script you would be among the first, who can use it.
-
Mateo about 9 yearsany word on where to "me too" this on launchpad?