Snippet for ~/.bash_profile, adding hostname autocomplete to ssh.
Extracts host hints from both ~/.ssh/config and /etc/hosts.
function __completeSSHHosts {
COMPREPLY=()
local currentWord=${COMP_WORDS[COMP_CWORD]}
local completeHosts=$(
cat "$HOME/.ssh/config" | \
grep --extended-regexp "^Host +([^* ]+ +)*[^* ]+ *$" | \
tr -s " " | \
sed -E "s/^Host +//"
)
COMPREPLY=($(compgen -W "$completeHosts" -- "$currentWord"))
return 0
}
complete -F __completeSSHHosts ssh