简体   繁体   English

如何在bash自动完成中仅显示第一级子目录?

[英]How to show only first level subdir in bash auto-completion?

I have this bash completion file 我有这个bash完成文件

#/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

When I have multiple choice and hit TAB twice I see following: 当我有多个选择并按两次TAB时,我看到以下内容:

$ mycommand Secondrootdir/
Secondrootdir/
Secondrootdir/Anothersubdir/
Secondrootdir/Anothersubdir/Subsubdir/
Secondrootdir/Thirdsubdir/

But I want to see only one-level deep subdirs of the current entered Secondrootdir 但我想看到目前进入Secondrootdir的只有一级深的子目录

like this: 像这样:

$ mycommand Secondrootdir/
Anothersubdir/ Thirdsubdir/

cd command does business well cd命令做得很好

$ cd Music/
kattymusic/ Playlists/  Relax/

But I can't understand how cd auto-completion works. 但我无法理解cd自动完成的工作原理。

May anybody help me, please? 请问有人帮帮我吗?

Many thanx to the post 很多人都不喜欢这个帖子

Bash completion: compgen a wordlist as if they were paths - Only suggest up until next slash Bash完成:compgen一个wordlist就好像它们是路径 - 只建议直到下一个斜线

finally the code looks like this 最后代码看起来像这样

#/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

I've found the solution and it's incredibly simple. 我找到了解决方案,而且非常简单。

Just one line of code does the magic 只需一行代码即可实现

# This forces readline to only display the last item separated by a slash
compopt -o filenames

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM