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 &&
    local cur opt

    # 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

    # Completion of command parameters.
    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.
        return 0

    # Commands which require .deb/.udeb file name.
        _filedir '?(u)deb'
        return 0
    # Commands which require package name.
        COMPREPLY=( $( apt-cache --no-generate pkgnames "$cur" \
        2> /dev/null ) )
        return 0

    # Commands which require name of installed package.
        if [ -f /etc/debian_version ]; then
        # Debian system
        COMPREPLY=( $( _comp_dpkg_installed_packages $cur ) )
        # assume RPM based
        return 0
} &&
complete -F _apt apt
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 0 guests