Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am using zsh with oh-my-zsh. Unfortunately, oh-my-zsh does not use file ~/.ssh/config for hostname auto-completion (see Issue #1009, for instance).

This could easily archived by the following code:

[ -r ~/.ssh/config ] && _ssh_config=($(cat ~/.ssh/config | sed -ne 's/Host[=\t ]//p')) || _ssh_config=()
zstyle ':completion:*:hosts' hosts $_ssh_config

However, if I add the above commands to my ~/.zshrc file, all other sources for hostnames (like ~/.ssh/known_hosts), which are defined in file ~/.oh-my-zsh/lib/completion.zsh, are overridden.

How can I append new completion rules for ':completion:*:hosts' in my ~/.zshrc file?

share|improve this question
up vote 3 down vote accepted

I think you need to retrieve the existing items and append yours.

zstyle -s ':completion:*:hosts' hosts _ssh_config
[[ -r ~/.ssh/config ]] && _ssh_config+=($(cat ~/.ssh/config | sed -ne 's/Host[=\t ]//p'))
zstyle ':completion:*:hosts' hosts $_ssh_config
share|improve this answer
Thanks, zstyle -s was the missing piece. – Martin Oct 18 '12 at 5:08

Another option is to redefine the hosts command (by default getent hosts) using zstyles. The advantage is that you can do it for ssh only and make it dynamic (except for the caching done by the completion system):

zstyle ':completion::complete:ssh*:*:hosts' command '
  getent hosts; sed -n "s/^Host[=[:blank:]]*/ignored /p" ~/.ssh/config'
share|improve this answer

The best option appears be to just use zsh's defaults.

See my pull request for oh-my-zsh at https://github.com/robbyrussell/oh-my-zsh/pull/1498

share|improve this answer

You may want to replace oh-my-zsh with the much-better written and maintained https://github.com/sorin-ionescu/prezto which uses default zsh completions and this external module https://github.com/zsh-users/zsh-completions

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.