Bash autocompletion for apt

Contribute code & patches
Forum rules
No support questions here please

Bash autocompletion for apt

Postby tomac on Wed Oct 05, 2011 5:23 pm

Hello all!

I installed LMDE Gnome edition three days ago, and noticed that 'apt' utility (the Linux Mint specific one, not apt-get and friends) lacks bash completion. Today I wrote a simple bash-completion script for it (mostly by copy-pasting code from 'apt' and 'dpkg' completion scripts). It can complete names of commands, packages and dpkg file names (in case of "deb" command). I thought someone may find it useful. If you want to give it a try, just copy&paste the following code to some editor (like 'gedit'), save it and then copy it to /etc/bash_completion.d directory as root, for example using the following command in terminal:

sudo cp apt-linux-mint /etc/bash_completion.d

(assuming that you saved script as "apt-linux-mint")
After opening new terminal window (or new tab in terminal) completion should work for 'apt'.

Code: Select all
#
# Bash completion file for Linux Mint apt utility.
#

have apt &&
_apt()
{
    local cur opt
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"

    # Completion of commands.
    if [[ $COMP_CWORD == 1 ]]; then
    COMPREPLY=( $(compgen -W '\
        autoclean autoremove build build-dep changelog check clean \
        contains content deb depends dist-upgrade download \
        dselect-upgrade held help hold install policy purge rdepends \
        reinstall remove search show source sources unhold update \
        upgrade version' "$cur" ) )
    return 0
    fi

    # Completion of command parameters.
    opt="${COMP_WORDS[1]}"
    case $opt in
    # Commands which require filename.
    # Note: "search" command does not necessarilly require
    # filename, it can accept any pattern, but I put it in this
    # group in order to allow filename-completion for this command.
    "contains"|"search")
        _filedir
        return 0
        ;;

    # Commands which require .deb/.udeb file name.
    "deb")
        _filedir '?(u)deb'
        return 0
        ;;
   
    # Commands which require package name.
    "build"|"build-dep"|"changelog"|"depends"|"download"|"install"|\
    "rdepends"|"show"|"source"|"version")
        COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
        2> /dev/null ) )
        return 0
        ;;

    # Commands which require name of installed package.
    "content"|"hold"|"purge"|"reinstall"|"remove"|"unhold")
        if [ -f /etc/debian_version ]; then
        # Debian system
        COMPREPLY=( $( _comp_dpkg_installed_packages $cur ) )
        else
        # assume RPM based
        _rpm_installed_packages
        fi
        return 0
        ;;
    esac
} &&
complete -F _apt apt
tomac
Level 1
Level 1
 
Posts: 2
Joined: Wed Oct 05, 2011 4:55 pm

Linux Mint is funded by ads and donations.
 

Return to Code & Patches

Who is online

Users browsing this forum: No registered users and 2 guests