Create on-the-fly hostname lists for ssh tab completion

Mar 24, '08 07:30:04AM

Contributed by: latigid

If you don't want to manage server lists for tab completion with ssh, put the following line in your .bash_profile file in your home directory:

complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh
This will allow you to tab complete any hostname you've previously ssh'd to.

[robg adds: This worked as described for me. The complete command is a built-in bash function that lets you specify lists of options to be used with tab completion on a given command. The version above parses your known_hosts to create the list of options. You can read more about the complete built-in function in the bash man pages -- man bash, then search for the section titled Programmable Completion.]

Comments (20)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20080317085050719