Back Original

Improving SSH autocomplete to zsh

Ok, small thing that had been annoying me. I use oh-my-zsh, and when you ssh n it autocompletes based on known hosts not what's in your ~/.ssh/config

I added the following to my dotfiles:

# Better SSH/Rsync/SCP Autocomplete
# _ssh_hosts skips ~/.ssh/config whenever known_hosts matches, so feed config Host aliases in directly
zstyle -e ':completion:*:(ssh|scp|rsync|sftp):*' hosts 'reply=(); local f l; for f in ~/.ssh/config(N) ~/.orbstack/ssh/config(N); for l in ${(f)"$(<$f)"}; [[ $l == (#i)[[:space:]]#host[[:space:]]##* ]] && reply+=(${${=l}[2,-1]:#(*[*?]*|!*)})'
zstyle ':completion:*:(scp|rsync):*' tag-order ' hosts:-ipaddr:ip\ address hosts:-host:host files'
zstyle ':completion:*:(ssh|scp|rsync):*:hosts-host' ignored-patterns '<->.<->.<->.<->' '*:*' loopback ip6-loopback localhost ip6-localhost broadcasthost
zstyle ':completion:*:(ssh|scp|rsync):*:hosts-ipaddr' ignored-patterns '^(<->.<->.<->.<->|(|::)([[:xdigit:].]##:(#c,2))##(|%*))' '127.0.0.<->' '255.255.255.255' '::1' 'fe80::*'

And now #ssh host aliases in .ssh/config take precedence!

/Nat

#cli #linux #shell

Responses

Nat? Nat. Nat!Nat? Nat. Nat!