SSH autocomplete on Mac OSX

I switched to Mac OSX few months ago, after using Linux as main OS for about 10 years. It wasn’t too difficult, the most difficult part was to find applications to replace those I was using, I will post a list of my preferred OSX applications someday. I also had to change some old habits I developed over the years (keyboard shortcuts, etc). However, it’s not the subject of my post today : I just read on a blog how to turn on SSH autocomplete on OSX so you don’t have to type (or remember) the complete names of the servers you defined in your config file.

It’s really simple, you just have to edit (or create) your .bash_profile file :

vi ~/.bash_profile

and add following lines :

_complete_ssh_hosts ()
{
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
comp_ssh_hosts=`cat ~/.ssh/known_hosts | \
cut -f 1 -d ' ' | \
sed -e s/,.*//g | \
grep -v ^# | \
uniq | \
grep -v "\[" ;
cat ~/.ssh/config | \
grep "^Host " | \
awk '{print $2}'
`
COMPREPLY=( $(compgen -W "${comp_ssh_hosts}" -- $cur))
return 0
}
complete -F _complete_ssh_hosts ssh

and finally :

source ~/.bash_profile

This modification will, not only search in your ~/.ssh/config file, but also in your known_hosts, so any host your already ssh’d into will be found with autocomplete.

Finally, I suggest you take a look at the original page at http://www.shocm.com/2011/01/ssh-autocomplete-on-osx/. Eric Van Johnson has an interesting blog and among other things he discusses Open Source.

Leave a Reply

Your email address will not be published. Required fields are marked *