Skip to content

Instantly share code, notes, and snippets.

@papaben
Created May 13, 2013 22:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save papaben/5571891 to your computer and use it in GitHub Desktop.
Save papaben/5571891 to your computer and use it in GitHub Desktop.
The bash completion file I'm using for ssh and scp
##
# Complete SSH with known hosts or configured hosts
##
_complete_ssh_hosts ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=$(if [ -f ~/.ssh/known_hosts ]; then
cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
fi
if [ -f ~/.ssh/config ]; then
cat ~/.ssh/config | \
grep "^Host " | \
awk '{print $NF}'
fi
)
COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
return 0
echo "$comp_ssh_hosts"
}
# Auto-complete with,
# * file names,
# * entries from hostfile,
# * results of _complete_ssh_hosts
complete \
-A file \
-A hostname \
-F _complete_ssh_hosts \
ssh scp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment