完成别名时不起作用 "properly"

Completion doesn't work "properly" when it is aliased

我正在 Ubuntu 16.04 LTS bash v4.3.46.

我将别名 "p" 设置为 "python3"。通过在 .bashrc

中写入
alias p='python3'

问题:

使用别名时无法正常补全。 (它也显示 python 的可执行文件)

$p <tab>
a.py   a.txt  b.py   b.txt  c.txt  hoge/

与普通命令比较:(这里只显示可执行文件)

$python3 <tab>
a.py  b.py  hoge/

我试过的:

当我检查 python3 的完成时,

$complete -p python3
complete -F _python python3

因此,我在.bashrc

中添加了以下内容
complete -F _python p 

现在,我得到了:

$complete -p p
complete -F _python p

它似乎有效,但是,我得到了与开始相同的结果。

--------已添加--------
我在 /usr/share/bash-completion/completions/python3

找到了 _python 的定义
# bash completion for python                               -*- shell-script -*-

_python_modules()
{
    COMPREPLY+=( $( compgen -W "$( ${1:-python} -c 'import pkgutil
for mod in pkgutil.iter_modules(): print(mod[1])' )" 2>/dev/null -- "$cur" ) )
}

_python()
{
    local cur prev words cword
    _init_completion || return

    case $prev in
        -'?'|-h|--help|-V|--version|-c)
            return 0
            ;;
        -m)
            _python_modules ""
            return 0
            ;;
        -Q)
            COMPREPLY=( $( compgen -W "old new warn warnall" -- "$cur" ) )
            return 0
            ;;
        -W)
            COMPREPLY=( $( compgen -W "ignore default all module once error" \
                -- "$cur" ) )
            return 0
            ;;
        !(?(*/)python*([0-9.])|-?))
            [[ $cword -lt 2 || ${words[cword-2]} != -@(Q|W) ]] \
                && _filedir
            ;;
    esac


    # if '-c' is already given, complete all kind of files.
    local i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${words[i]} == -c ]]; then
            _filedir
        fi
    done


    if [[ "$cur" != -* ]]; then
        _filedir 'py?([co])'
    else
        COMPREPLY=( $( compgen -W '$( _parse_help "" -h )' -- "$cur" ) )
    fi

    return 0
} &&
complete -F _python python python2 python3

# ex: ts=4 sw=4 et filetype=sh

编辑这个似乎没问题。

问题在于此 case 语句:!(?(*/)python*([0-9.])|-?)) 将制表符补全限制为仅 aliases/commands 其中包含单词 python

如果您将 case 语句更改为:!(*|-h)) 甚至 !(p|-h)),那么您的自动完成应该可以工作。

我个人只是将修改后的代码添加到我的 .bashrc...如下所示,只需重命名函数并进行别名处理:

# bash completion for python                               -*- shell-script -*-

_python_modules_cust()
{
    COMPREPLY+=( $( compgen -W "$( ${1:-python} -c 'import pkgutil
for mod in pkgutil.iter_modules(): print(mod[1])' )" 2>/dev/null -- "$cur" ) )
}

_python_cust()
{
    local cur prev words cword
    _init_completion || return

    case $prev in
        -'?'|-h|--help|-V|--version|-c)
            return 0
            ;;
        -m)
            _python_modules_cust ""
            return 0
            ;;
        -Q)
            COMPREPLY=( $( compgen -W "old new warn warnall" -- "$cur" ) )
            return 0
            ;;
        -W)
            COMPREPLY=( $( compgen -W "ignore default all module once error" \
                -- "$cur" ) )
            return 0
            ;;
        !(*|-?))
            [[ $cword -lt 2 || ${words[cword-2]} != -@(Q|W) ]] \
                && _filedir
            ;;
    esac


    # if '-c' is already given, complete all kind of files.
    local i
    for (( i=0; i < ${#words[@]}-1; i++ )); do
        if [[ ${words[i]} == -c ]]; then
            _filedir
        fi
    done


    if [[ "$cur" != -* ]]; then
        _filedir 'py?([co])'
    else
        COMPREPLY=( $( compgen -W '$( _parse_help "" -h )' -- "$cur" ) )
    fi

    return 0
} &&
alias p='python3' &&
complete -F _python_cust p

# ex: ts=4 sw=4 et filetype=sh