Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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

4 Answers 4

up vote 2 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

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

See my pull request for oh-my-zsh at

share|improve this answer

You may want to replace oh-my-zsh with the much-better written and maintained which uses default zsh completions and this external module

share|improve this answer

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

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.