linux - 如何在 bash 自动完成中只显示一级子目录?

标签 linux bash shell ubuntu bash-completion

我有这个 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

当我有多项选择并按 TAB 键两次时,我会看到以下内容:

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

但我只想查看当前输入的 Secondrootdir

的一级深层子目录

像这样:

$ mycommand Secondrootdir/
Anothersubdir/ Thirdsubdir/

cd 命令做的很好

$ cd Music/
kattymusic/ Playlists/  Relax/

但我不明白 cd 自动完成是如何工作的。

有人可以帮帮我吗?

最佳答案

非常感谢帖子

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

最后的代码是这样的

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

关于linux - 如何在 bash 自动完成中只显示一级子目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43262370/

相关文章:

c - 在从 Linux 套接字发送/接收数据包后访问辅助数据时,在哪些情况下 msg_controllen 可以为 0?

bash - 按列消除部分重复行并保留最后一行

python - 如何将 Python 程序的标准输出移至文件?

shell - 构建批处理文件以按顺序运行 exe 文件

linux - 如何将这个 bash 脚本移植到 sh?

mongodb - 配置/增加 MongoDB shell 历史长度

C非阻塞键盘输入

linux - 通过 CGI 执行 bash 脚本

linux - find 命令在 zsh 和 bash 中的工作方式不同

bash - 管道输出到两个不同的命令