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 --regexp "^Host +[^* ]+? *$" | \
tr -s " " | cut -d " " -f 2;
cat /etc/hosts | \
grep --extended-regexp --regexp "^[0-9]{3}\." | \
awk "{print \$2}"
)
COMPREPLY=($(compgen -W "$completeHosts" -- "$currentWord"))
return 0
}
complete -F _completeSSHHosts ssh