1

I my bashrc. I have a bash completion for the command scp implemented as follows

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

The idea is that when pressing scp [tab] I see all files in the current directory and the words listed in the text file ~/.ssh_complete. Let's assume this file contain the following entries:

alex@192.0.0.1
alex@192.0.0.2

The desired behavior is as follows: I type scp alex@ [tab] and tab completion'completes' the command to scp alex@192.0.0. automatically, because there are only two possible arguments starting with alex@ (assuming there is no file named similar to that in the currect working directory.).

The behavior I get with the current implementation is as follows: I type scp alex@ [tab] and the tab completion does not complete anything, but lists every possible argument below the command.

How can I get the desired behavior?

1

This works for me:

_foo() 
{
  local cur
  COMPREPLY=()
  cur=${COMP_WORDS[COMP_CWORD]}
  opts=""
  while read line           
  do           
    opts+=" $line"
  done < ~/.ssh_complete
  opts+=" "
  opts+=$(find . ! -name . -prune -type f)
  case "$cur" in
    *)
    COMPREPLY=( $( compgen -W '$opts' -- $cur ) );;
  esac

  return 0
}

complete -F _foo scp
  • @Alex: If this answer solved your problem, please mark it as accepted by clicking the check mark. – Mechanical snail Feb 7 '13 at 0:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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