2013-02-06 46 views
1

在我的bashrc中。我想打壞完成命令scp如下爲什麼這個自動完成功能不能在Linux中自動完成?

function _scp_complete 
{ 
    COMPREPLY="" 
    COMPREPLY+=($(cat ~/.ssh_complete)) 
    COMPREPLY+=($(find . ! -name . -prune -type f)) 
} 
complete -F _scp_complete scp 

的想法是,按scp [tab]當我看到在當前目錄下的所有文件文本文件~/.ssh_complete列出的單詞。讓我們假設該文件包含以下項目:

[email protected] [email protected]

期望的行爲如下:I型scp [email protected][TAB]和標籤completion'completes'命令SCP的亞歷克斯@ 192.0.0。自動,因爲只有兩個開始與亞歷克斯@可能的參數(假設在當期的工作目錄中沒有類似命名的文件。):

>scp [email protected][TAB] 
    [email protected] [email protected] 
>scp [email protected] 

行爲我所描述的實施得到如下:I型scp [email protected][TAB]和標籤完成確實不完整什麼,但列出了下面的命令,每一個可能的論點:

>scp [email protected][TAB] 
    [email protected] [email protected] file1 Music Pictures ./.emacs <ALL files in the current directory> 
>scp [email protected] 

我怎樣才能修復功能,以獲得所需的行爲嗎?

回答

1

您需要使用COMP_WORDS數組來獲取已輸入的當前單詞。然後使用compgen命令根據您的原始單詞列表生成可能的完成。

嘗試以下操作:

_scp_complete() 
{ 
    local cur=${COMP_WORDS[COMP_CWORD]} 
    COMPREPLY=($(compgen -W "$(< ~/.ssh_complete) $(find . ! -name . -prune -type f)" -- $cur)) 
} 
complete -F _scp_complete scp 

看看這個博客帖子獲取更多詳情:Writing your own Bash Completion Function

注意,我不認爲這將完成上的文件名稱中有空格。

另請注意,使用$(< file)從文件中提取文本效率更高,而不是$(cat file)

+0

感謝您的幫助和解釋! – Alex