Use Tab-completion for SSH 'host' aliases

Jan 15, '10 07:30:00AM

Contributed by: Anonymous

Do you use just use 'host' aliases defined in ~/.ssh/config for all of your SSH'ing? If so, this hint lets you easily tab-complete those aliases in Bash without needing bash-completion.

It's quick. It's dirty. But it works.

Put the following in your user's .profile (or Bash-specific initialization file):

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)" scp sftp ssh
Now any time you're using scp, sftp, or ssh, you can just type part of the alias. For instance, on my computer, I type ssh se[TAB], and it completes to ssh server.

[robg adds: I haven't tested this one.]

Comments (6)

Mac OS X Hints