首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在bash自动完成中只显示一级子How?

如何在bash自动完成中只显示一级子How?
EN

Stack Overflow用户
提问于 2017-04-06 17:48:22
回答 2查看 172关注 0票数 3

我有这个bash完成文件

代码语言:javascript
复制
#/etc/bash_completion.d/mycommand

_mycommand()
{
    local cur
    COMPREPLY=()

    _init_completion || return

    #Variable to hold the current word
    cur="${COMP_WORDS[COMP_CWORD]}"

    #array variable COMPREPLY

    dirs=('Rootdir/
        Secondrootdir/ 
        Rootdir/Subdir/
        Secondrootdir/Anothersubdir/
        Secondrootdir/Thirdsubdir/
        Secondrootdir/Anothersubdir/Subsubdir/'
    )

    COMPREPLY=($(compgen -W "$dirs" "$cur"))

}

#Assign the auto-completion function _mycommand for our command mycommand.
complete -F _mycommand mycommand

当我有多项选择并两次点击TAB时,我会看到以下情况:

代码语言:javascript
复制
$ mycommand Secondrootdir/
Secondrootdir/
Secondrootdir/Anothersubdir/
Secondrootdir/Anothersubdir/Subsubdir/
Secondrootdir/Thirdsubdir/

但是我只想看到当前进入的只有一个级别的深子列,即Secondrootdir

就像这样:

代码语言:javascript
复制
$ mycommand Secondrootdir/
Anothersubdir/ Thirdsubdir/

cd命令做得很好

代码语言:javascript
复制
$ cd Music/
kattymusic/ Playlists/  Relax/

但我不明白cd自动完成是怎么工作的。

有人能帮我吗?

EN

回答 2

Stack Overflow用户

发布于 2017-04-08 14:24:54

很多人到了岗位上

Bash completion: compgen a wordlist as if they were paths - Only suggest up until next slash

最后,代码如下所示

代码语言:javascript
复制
#/etc/bash_completion.d/mycommand

_mycommand()
{
    local cur i

    _init_completion || return

    compopt -o filenames

    #Variable to hold the current word
    cur="${COMP_WORDS[COMP_CWORD]}"


    dirs='Rootdir/Subdir/ Secondrootdir/Thirdsubdir/ Secondrootdir/Anothersubdir/Subsubdir/'

    # If we include root dirs we have to remove them
    # e.g we have Secondrootdir/ Secondrootdir/Anothersubdir/ Secondrootdir/Anothersubdir/Subsubdir/'
    # we need to skip the shorties path and remain only the longest one Secondrootdir/Anothersubdir/Subsubdir/'
    # dirs='Rootdir/ 
    #     Secondrootdir/
    #     Rootdir/Subdir/
    #     Secondrootdir/Anothersubdir/
    #     Secondrootdir/Thirdsubdir/
    #     Secondrootdir/Anothersubdir/Subsubdir/'

    arr=($(compgen -W "$dirs" -- $cur))
    for path in "${arr[@]}"
    do

        local trailing_trim

        # Determine what to trim from the end
        trailing_trim="${path#${cur%/*}/}/"
        trailing_trim="${trailing_trim#*/}"
        trailing_trim="${trailing_trim%/}"


        # Don't add a space if there is more to complete
        [[ "$trailing_trim" != "" ]] && compopt -o nospace

        # Remove the slash if mark-directories is off
        if ! _rl_enabled mark-directories
        then
            # If The current typed path doesnt have a slash in it yet check if
            # it is the full first portion of a path and ignore everything after
            # if it is. We don't have to do this once the typed path has a slash
            # in it as the logic above will pick up on it
            [[ "$cur" != */* && "$path" == ${cur}/* ]] && path="$cur/$trailing_trim"    

            trailing_trim="/$trailing_trim"
        fi

        COMPREPLY[i++]="${path%%${trailing_trim}}"

    done



}

#Assign the auto-completion function _mycommand for our command mycommand.
complete -F _mycommand mycommand
票数 1
EN

Stack Overflow用户

发布于 2017-04-08 09:49:31

我找到了解决办法,而且非常简单。

只需一行代码就能产生魔力

代码语言:javascript
复制
# This forces readline to only display the last item separated by a slash
compopt -o filenames
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43262370

复制
相关文章

相似问题

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