Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Use Tab-completion for SSH 'host' aliases UNIX
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.]
  • Currently 3.19 / 5
  You rated: 1 / 5 (26 votes cast)

Use Tab-completion for SSH 'host' aliases | 6 comments | Create New Account
Click here to return to the 'Use Tab-completion for SSH 'host' aliases' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
For zsh
Authored by: sabi on Jan 15, '10 08:57:00AM
I've had this set up for years...

# completion of .ssh/hosts
_hosts() {
    local expl hosts

    if ! zstyle -a ":completion:${curcontext}:hosts" hosts hosts
	(( $+_cache_hosts )) ||
	: ${(A)_cache_hosts:=${(s: :)${(ps:\t:)${${(f)~~"$(</etc/hosts)"}%%\#*}##[:blank:]#[^[:blank:]]#}}} 
	(( $+_cache_sshconf_hosts )) ||
	: ${(A)_cache_sshconf_hosts:=${${${(M)${(s:# :)${(zj:# :)${(Lf)"$([[ -f ~/.ssh/config ]] && <~/.ssh/config)"}%%\#*}}##host *}#host }/\*}}
	hosts=( "$_cache_hosts[@]" "$_cache_sshconf_hosts[@]")
	_wanted hosts expl host \
	    compadd -M 'm:{a-zA-Z}={A-Za-z} r:|.=* r:|=*' "$@" -a hosts

[ Reply to This | # ]
Use Tab-completion for SSH 'host' aliases
Authored by: Chris M Hartman on Jan 15, '10 10:00:23AM

This was covered last year here:

See the comments there for aliases for other shells.

[ Reply to This | # ]
Better, broader solution: install bash_completion
Authored by: babbage on Jan 15, '10 10:25:00AM

Here's a better idea -- just install bash_completion:

It does all kinds of clever things, with hostname completion just being the start of it. It's also clever about things like handling svn/cvs commands & paths, handling archives, etc.

I've had it installed via Fink for a while now, but if you don't care for Fink, you can always just download the raw source tarball from here:

The README document describes how to set it up, but it's mainly just a matter of putting it somewhere sensible (/etc) and then editing your ~/.basrc file to load it at launch time.



[ Reply to This | # ]
Use Tab-completion for SSH 'host' aliases
Authored by: boothefox on Jan 15, '10 01:07:43PM

For frequent connecting it is easier to make an app via Applescript Editor to call Terminal and execute an "ssh -l user -p port host" and save it somewhere convenient.

[ Reply to This | # ]
Use Tab-completion for SSH 'host' aliases
Authored by: pseutro on Jan 21, '10 03:40:43PM
If you don't mind maintaining a separate file with host names, this works pretty well in bash.
$ cat ~/.hosts
$ export HOSTFILE=~/.hosts
$ complete -A hostname   rsh rcp telnet rlogin r ftp ping disk ssh

[ Reply to This | # ]
Speed things up with Quicksilver
Authored by: Skurfer on Jan 27, '10 01:26:51PM

I've used a .hosts file for completion for years, but switching to Terminal and opening a new window? Who has time for all that nonsense? So, I wrote a Quicksilver plug-in to speed things along. Just type a couple of characters of the hostname (they don't have to be contiguous) and hit ↩.

Look under Downloads for an already-built-and-ready-to-install copy.

[ Reply to This | # ]