首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在别名化时,完成不能“正确”工作

在别名化时,完成不能“正确”工作
EN

Stack Overflow用户
提问于 2017-04-18 05:00:30
回答 1查看 216关注 0票数 2

我在Ubuntu16.04LTS上使用bash v4.3.46。

我将别名"p“设置为"python3”。通过在.bashrc中写这个

代码语言:javascript
复制
alias p='python3'

问题:

当别名化时,完成不能正常工作。(它也显示了python的可执行文件)

代码语言:javascript
复制
$p <tab>
a.py   a.txt  b.py   b.txt  c.txt  hoge/

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

代码语言:javascript
复制
$python3 <tab>
a.py  b.py  hoge/

我尝试了什么:

当我检查python3的完成时,

代码语言:javascript
复制
$complete -p python3
complete -F _python python3

因此,我在.bashrc中添加了以下内容

代码语言:javascript
复制
complete -F _python p 

现在,我得到:

代码语言:javascript
复制
$complete -p p
complete -F _python p

这似乎是有效的,然而,我得到了同样的结果与开始。

我在_python上找到了/usr/share/bash-completion/completions/python3的定义

代码语言:javascript
复制
# 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 "$1"
            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 "$1" -h )' -- "$cur" ) )
    fi

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

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

编辑这个似乎没问题。

  • 你能告诉我我该怎么编辑这个吗?
  • 要编辑这个,我需要sudo,还有其他不使用sudo的方法吗? 谢谢。
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-25 03:39:32

问题是,这个case语句:!(?(*/)python*([0-9.])|-?))将制表符完成限制为只包含单词python的别名/命令。

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

我会亲自将修改后的代码添加到我的.bashrc中.如下所示,只需重命名函数并执行混叠操作:

代码语言:javascript
复制
# 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 "$1"
            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 "$1" -h )' -- "$cur" ) )
    fi

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

# ex: ts=4 sw=4 et filetype=sh
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43463902

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档