Bug 1227119 - ZSH does not properly autocomplete ssh Host entries
Summary: ZSH does not properly autocomplete ssh Host entries
Status: IN_PROGRESS
Alias: None
Product: openSUSE Tumbleweed
Classification: openSUSE
Component: Other (show other bugs)
Version: Current
Hardware: Other Other
: P5 - None : Normal (vote)
Target Milestone: ---
Assignee: Paolo Perego
QA Contact: E-mail List
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-06-27 08:16 UTC by Filippo Bonazzi
Modified: 2024-07-09 14:39 UTC (History)
0 users

See Also:
Found By: ---
Services Priority:
Business Priority:
Blocker: ---
Marketing QA Status: ---
IT Deployment: ---


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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