Skip to content

Instantly share code, notes, and snippets.

@lenidh
Created April 24, 2014 13:05
Show Gist options
  • Save lenidh/11253918 to your computer and use it in GitHub Desktop.
Save lenidh/11253918 to your computer and use it in GitHub Desktop.
bash completion for apt command
# Debian apt(8) completion -*- shell-script -*-
_apt()
{
local cur prev words cword
_init_completion || return
local dashoptions='-h --help -v --version -c --config-file -o --option -t -a'
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) ]]; then
special=${words[i]}
fi
done
if [[ -n $special ]]; then
case $special in
install|show)
COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
2> /dev/null ) )
return 0
;;
remove)
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
;;
esac
fi
case $prev in
# don't complete anything if these options are found
search|update|upgrade|full-upgrade)
return 0
;;
list)
COMPREPLY=( $( compgen -W '--installed --upgradable --all-versions' -- "$cur") )
return 0
;;
-c)
_filedir
return 0
;;
-t)
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 "$dashoptions" -- "$cur" ) )
else
COMPREPLY=( $( compgen -W 'list search show update install remove
upgrade full-upgrade edit-sources' -- "$cur" ) )
fi
return 0
} &&
complete -F _apt apt
# ex: ts=4 sw=4 et filetype=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment