fix(ssh): update host autocompletion

The current completion doesn't work correctly when there are multiple hosts
per line, e.g.:

```
Host foo bar baz
  ...
```

SSH config also allows to have repeated hosts, e.g.:

```
Host foo bar
  ...

Host foo
  ...
```

With this commit we split all the host values in lines, sort them out and then
make a unique array of them.
This commit is contained in:
Gam 2024-02-20 08:31:55 -06:00 committed by G G
parent 40ff950fcd
commit 0f52e71168

View file

@ -4,9 +4,16 @@
# Filter out wildcard host sections.
_ssh_configfile="$HOME/.ssh/config"
if [[ -f "$_ssh_configfile" ]]; then
_hosts=($(egrep '^Host.*' "$_ssh_configfile" | awk '{print $2}' | grep -v '^*' | sed -e 's/\.*\*$//'))
zstyle ':completion:*:hosts' hosts $_hosts
unset _hosts
_ssh_hosts=($(
egrep '^Host.*' "$_ssh_configfile" |\
awk '{for (i=2; i<=NF; i++) print $i}' |\
sort |\
uniq |\
grep -v '^*' |\
sed -e 's/\.*\*$//'
))
zstyle ':completion:*:hosts' hosts $_ssh_hosts
unset _ssh_hosts
fi
unset _ssh_configfile