How to autocomplete ssh <tab> with data from ~/.ssh/config?

Copy/paste the following into ~/.bash_profile:

1
complete -o default -o nospace -W "$(/usr/bin/env ruby -ne 'puts $_.split(/[,\s]+/)[1..-1].reject{|host| host.match(/\*|\?/)} if $_.match(/^\s*Host\s+/);' < $HOME/.ssh/config)"

You’ll then need to either restart your terminal or type source ~/.bash_profile (note that the latter will only work in your current tab).