Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have set auto-completion in my .bashrc as follows:

complete -W "$(echo `cat ~/.ssh_complete`;)" scp

but this ONLY used the words given in the file ~/.ssh_complete. How is it possible to extend the definition so tab-completion on scp also uses the names of files in the local directory.

A simple solution to my problem would do (filenames AND words in wordlist), I do not require tab-completion to recognize the format/details of the actual scp command. Again: I want the tab-completion to show the names of the files in the current directory AND to show the entries given in the file ~/.ssh_complete.

When there is a new suggestion I edit the file .bashrc, source it, and try the tab-completion on scp again.

share|improve this question
add comment

2 Answers

up vote 0 down vote accepted
+50

ok if you write a function as below

function _scp_complete
{

 COMPREPLY=""
 COMPREPLY+=( $(cat ~/.ssh_complete ) )
 COMPREPLY+=( $( ls ) )

}

then

complete -F _scp_complete scp

the -F to complete means run the function indicated (_scp_complete) then use the array COMPREPLY with each element as one completion

In the function I reset COMPREPLY to be blank Then I cat out the ~.ssh_complete ( each line becomes one element of COMPREPLY ) then add in the output of ls.

share|improve this answer
    
Your answer is almost the solution. As you can copy only files, I modified the line listing the directory to list only files: COMPREPLY+=( $( find . ! -name . -prune -type f ) ). Now I am happy... –  Alex Jan 23 '13 at 6:21
add comment

As documented, you can combine the two with:

complete -o <comp-options> -W "$(cat ~/.ssh_complete)" scp

Where <comp-options> allows you to specify which type of behaviour you want.

I got rid of the ridiculous UUoC and echo for you.

share|improve this answer
    
Thanks for your answer, but the behavior is not as expected. When I type scp and press TAB once, it completes to scp /home/alexander/.ssh_complete. I was expecting to see the list of files from the current directory AND the words in the given wordlist, not the name of the wordlist (I used filenames for the comp-options). –  Alex Jan 16 '13 at 10:32
    
My bad; answer edited to correct the wordlist –  adaptr Jan 16 '13 at 10:33
    
Now only the word in the wordlist are taken into account for tab completion. But none of the files in the current directory. –  Alex Jan 16 '13 at 10:38
    
As documented, -o only applies "if the compspec generated no matches". Filesystem completion seems to go last, except in the case of plusdirs. Why not add an -F that produces a glob of the CWD ? –  adaptr Jan 16 '13 at 10:52
    
have you ran: ". /etc/bash_completion" before that (assuming is a Debian like distro) –  jet Jan 16 '13 at 11:13
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.