Bug 1227119

Summary: ZSH does not properly autocomplete ssh Host entries
Product: [openSUSE] openSUSE Tumbleweed Reporter: Filippo Bonazzi <filippo.bonazzi>
Component: OtherAssignee: Paolo Perego <paolo.perego>
Status: IN_PROGRESS --- QA Contact: E-mail List <qa-bugs>
Severity: Normal    
Priority: P5 - None    
Version: Current   
Target Milestone: ---   
Hardware: Other   
OS: Other   
Whiteboard:
Found By: --- Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---

Description Filippo Bonazzi 2024-06-27 08:16:01 UTC
I have the following entry in my .ssh/config file:

Host somehost
	HostName somehost.xxx.yyy
	User fbonazzi
	preferredauthentications publickey

if I type ssh some<TAB>, ZSH will autocomplete

$ ssh somehost.xxx.yyy

instead of

$ ssh somehost

This ignores the config entry, so e.g. the right user and public key configured for the host are not used.

There must be something wrong in the ZSH ssh completion file:

/usr/share/zsh/functions/Completion/Unix/_ssh
Comment 1 Paolo Perego 2024-07-03 15:28:29 UTC
Try adding this in your .zshrc file as suggested in [0].
This solved the issue for me:

h=()
if [[ -r ~/.ssh/config ]]; then
  h=($h ${${${(@M)${(f)"$(cat ~/.ssh/config)"}:#Host *}#Host }:#*[*?]*})
fi
if [[ -r ~/.ssh/known_hosts ]]; then
  h=($h ${${${(f)"$(cat ~/.ssh/known_hosts{,2} || true)"}%%\ *}%%,*}) 2>/dev/null
fi
if [[ $#h -gt 0 ]]; then
  zstyle ':completion:*:ssh:*' hosts $h
  zstyle ':completion:*:slogin:*' hosts $h
fi

[0]: https://serverfault.com/questions/170346/how-to-edit-command-completion-for-ssh-on-zsh/170481#170481
Comment 2 Filippo Bonazzi 2024-07-09 14:39:18 UTC
Looks like that fixes the issue. Not sure if it's the cleanest way to apply this fix or if you have a better solution in mind, now that you know that this works