Skip to content

Instantly share code, notes, and snippets.

@andrewgross
Created December 13, 2011 18:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrewgross/1473356 to your computer and use it in GitHub Desktop.
Save andrewgross/1473356 to your computer and use it in GitHub Desktop.
# ssh-known-hosts completion
#
_ssh_known_hosts()
{
local cur prev opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
if [ -n "$(echo ${cur} | grep '@')" ] ; then
root_word=$(echo ${cur} |awk -F@ '{print $1}')
partial=$(echo ${cur} | tr -d ${root_word}@)
opts="$(cat ~/.ssh/known_hosts | awk '{print $1}' | egrep -v '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$' | awk -F\, '{print $1}' | uniq | sort)"
tmp=""
for opt in ${opts}
do
tmp="${tmp} ${root_word}@${opt}"
done
opts=${tmp}
COMPREPLY=( $(compgen -W "${opts}" ${cur}) )
return 0
fi
}
complete -F _ssh_known_hosts ssh
complete -F _ssh_known_hosts scp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment