Skip to content

Instantly share code, notes, and snippets.

@simonmcc
Created December 9, 2014 14:39
Show Gist options
  • Save simonmcc/24a3cba850eb3d9592b3 to your computer and use it in GitHub Desktop.
Save simonmcc/24a3cba850eb3d9592b3 to your computer and use it in GitHub Desktop.
# 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
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/@*/}@ -- ${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
complete -o bashdefault -o default -o nospace -F _ssh hpssh 2>/dev/null \
|| complete -o default -o nospace -F _ssh hpssh
#complete -f -F _ssh scp
#complete -F _ssh host sftp ssh sshgr
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment