Skip to content

Instantly share code, notes, and snippets.

@sashazykov
Last active August 29, 2015 14:03
Show Gist options
  • Save sashazykov/29b87b6b86e9ed3d75df to your computer and use it in GitHub Desktop.
Save sashazykov/29b87b6b86e9ed3d75df to your computer and use it in GitHub Desktop.
Bash completion for ssh. Tested with Mac OS X 10.9
# Add bash completion for ssh: it tries to complete the host to which you
# want to connect from the list of the ones contained in ~/.ssh/known_hosts
__ssh_known_hosts() {
if [[ -f ~/.ssh/known_hosts ]]; then
cut -d " " -f1 ~/.ssh/known_hosts | cut -d "," -f1 | grep -v "[\[\|]" | uniq | sort
fi
}
_ssh() {
local cur known_hosts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
known_hosts="$(__ssh_known_hosts)"
if [[ ! ${cur} == -* ]] ; then
if [[ ${cur} == *@* ]] ; then
COMPREPLY=( $(compgen -W "${known_hosts}" -P @ -- ${cur/*@/}) )
else
COMPREPLY=( $(compgen -W "${known_hosts}" -- ${cur}) )
fi
fi
return 0
}
complete -o bashdefault -o default -o nospace -F _ssh ssh 2>/dev/null \
|| complete -o default -o nospace -F _ssh ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment