Open or create ~/.bash_profile
and add the following lines:
# ssh autocomplete
function _ssh_autocomplete() {
COMPREPLY=()
CURRENT_WORD="${COMP_WORDS[COMP_CWORD]}"
if [[ ${CURRENT_WORD} == -* || ${COMP_CWORD} -eq 1 ]] ; then
WORD_LIST=$(sed -nE "s/Host\ ([a-zA-Z0-9].+)/\1/p" ~/.ssh/config | tr '\n' ' ' )
COMPREPLY=( $(compgen -W "$WORD_LIST" -- ${CURRENT_WORD}) )
fi
}
complete -F _ssh_autocomplete ssh
This will allow you to list hosts defined in ~/.ssh/config
by typing ssh <tab>
in the Terminal