How to show only first level subdir in bash autocomplete?

I have this bash completion file

#/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 press TAB twice, I see the following:

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

      

But I only want to see the siblings of the currently entered Secondrootdir

like this:

$ mycommand Secondrootdir/
Anothersubdir/ Thirdsubdir/

      

cd team works well in business

$ cd Music/
kattymusic/ Playlists/  Relax/

      

But I can't figure out how the cd autocomplete works.

Can someone help me please?

+3


source to share


2 answers


Lots of thanx to post

Bash completion: copy the list of words as if they were paths - only offer up to the next slash



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

      

+1


source


I found a solution and it is 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

      

0


source







All Articles