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

自14.04开始在Ubuntu中出现的新apt命令似乎是apt-getapt-cache之间非常有用的function交叉,但当前版本的bash-completion却不知道它…这使得它很多更难使用。

有没有快速的方法将此function添加到Bash以使apt命令易于使用?

这是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 

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

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

显然,如果一个bash-completion维护者想要抓住上述内容,那么你会受到GPL的欢迎(尽管我很想从一个新的文章中开始记录!)

为什么不使用原始的 bash-completion ?

试试这个脚本。 它将在~/tmp/bash-completion上下载并安装~/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 

您可以使用命令开始使用它. ~/tmp/bash-completion/bash_completion.sh . ~/tmp/bash-completion/bash_completion.sh ,可以放入~/.bashrc文件,或者 – 更好 – 将它符号链接到/etc/profile.d/目录中的某个文件中。 卸载原始bash-completion,因此您不会同时加载两者。