我可以为新的 apt 命令启用 bash-completion 吗?

Oli*_*Oli 20 apt bash auto-completion

新的apt命令,目前在Ubuntu 14.04以来,似乎是之间的功能一个真正有用的交集apt-getapt-cache,但当前版本的bash-completion不知道它...这使得它成为很多很难使用。

是否有一种快速的方法可以将此功能添加到 Bash 以使apt命令易于使用?

Oli*_*Oli 26

这是bash-complete包中的一个遗漏,而不是apt. 似乎还没有完成,所以我已经为apt命令拼凑了我所能做的(这不是有史以来最好的记录命令!)

以下是对现有apt-get完成的改编(从 的完成中删除元素并添加位apt-cache)。运行sudoedit /usr/share/bash-completion/completions/apt并粘贴以下内容:

# 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
Run Code Online (Sandbox Code Playgroud)

然后运行source ~/.bashrc加载完成。然后apt show firef+Tab应该完成。

这可能会为您提供不再存在的选项。我想我已经确定了主要命令(可能会随着时间的推移而改变),但至少它会帮助你使用常用命令:list search show update install remove upgrade full-upgrade edit-sources dist-upgrade purge.

显然,如果 bash-completion 维护者想要获得上述内容,欢迎您在 GPL 下使用它(尽管我很想从新的apt记录开始!)

  • 打开一个错误并将其作为补丁提交! (8认同)