8

zsh is great so far.
I am using zsh-completions but still I am unable to get autocompletion for ssh commands like in bash as shown in below screenshot:

BASH vs ZSH

How to get hostnames from /etc/hosts for ssh | scp | telnet command autocompletion in zsh shell ?

Update 1:

https://github.com/sunlei/zsh-ssh : This SSH completion offers a greater array of features in comparison to the default SSH completion.

1 Answer 1

9

Zsh won't offer you host completions until you enable its full completion system. You can do so by adding the following to your .zshrc file:

autoload -Uz compinit
compinit

Once initialized, Zsh's completion system will retrieve host names from /etc/hosts, /etc/ssh/ssh_known_hosts, ~/.ssh/known_hosts and Host .../Name ... lines in ssh config files. Have a look inside those files.

Note that if you have aliased ssh to another command, zsh will offer completions for that other command. If you want it to offer the same completions for that other command as those for ssh, you need to tell it to do so, with something like this:

alias ssh=<some other command>
compdef _ssh <some other-command>=ssh

Alternatively, you can use a function instead of an alias:

ssh() {
  <some other-command> "$@"
}
8
  • yes... I have this already in my .zshrc file. Do I need to add any zstyle for ssh? @MarlonRichert
    – Akhil
    Jun 18, 2020 at 4:20
  • No, it should just work. I tried this in Zsh without loading any other config and it works fine. Have you checked the contents of the hosts files I listed above? Jun 18, 2020 at 9:04
  • You can try this yourself: Do cd "$(mktemp -d)"; ZDOTDIR=$PWD zsh -df to start a sub shell without any config. In there, run autoload -Uz compinit && compinit. Then type ssh and press Tab. If it works there, then you must have something in your .zshrc that messes it up. Try commenting out lines in your .zshrc until you find what causes it. Jun 18, 2020 at 9:10
  • 1
    you are right. the problem is i have alias ssh='sshrc' in my zshrc. thanks.
    – Akhil
    Jun 18, 2020 at 11:36
  • 2
    _ssh handles scp, sftp etc so will be confused by sshrc. Try compdef _ssh sshrc=ssh to treat sshrc like ssh.
    – okapi
    Jun 19, 2020 at 21:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .