2017-04-06 73 views
3

我有這樣的bash補全文件如何僅在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自動完成的作品。

有人可以幫我嗎?

+0

號他們是遠程服務器。我只能使用API​​接收他們的列表。 – Oleg

+0

謝謝。很好的例子,但我想要相反的事情。 – Oleg

+0

我會在這方面付出更大的努力,並希望我能成功。 – Oleg

回答

0

我找到了解決方案,它非常簡單。代碼

只有一條線路做魔術

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

許多感謝名單的帖子

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 
+0

你可以接受你自己的答案。 – pynexj

相關問題