Score:0

Use `known-hosts-files` for `zsh` ssh command completion

in flag

This question was originally asked on SO, which is incorrect. As I was not able to (get it) moved to serverfault, I am re-asking it.

I am trying to configure tab completion for the ssh command in zsh to use an additional known_hosts file (which not located in ~/.ssh). By additional I mean I still want to use the "default" known_hosts file.

Currently, when I hit TAB after ssh, I get a list of hosts which seems to come from ~/.ssh/known_hosts and (probably) /etc/hosts. I would like to also get the hosts listed in ~/other/path/known_hosts.

I found a lot of material online, but somehow I cannot wrap my head around this one. The zsh webiste for example lists a style called known-hosts-files which sounds very promising. Also when I see how it is used it seems quite to the point.

But I cannot figure out how to use known-hosts-files.

I tried, for example, the following in my .zshrc:

zstyle ':completion:*:*:ssh:*:*' known-hosts-files '~/other/path/known_hosts'

and many variations of it to no avail.

For good measure I have also added (in my .zshrc):

zstyle ":completion:*:commands" rehash 1

but this did not change anything.

There is an older answer here on serverfault and another answer) tackling the same question, but I do not fully understand them and it seems that known-hosts-files should do exactly what I want?

I also use Oh-my-zsh, if that matters...

Score:0
in flag

Well, finally I found the culprit: quotes.

When setting known-hosts-files to just a single file (like in my example), the quotes can be used, but not with ~!

Adding this to .zshrc works as intended (macOS):

zstyle ':completion:*:*:ssh:*:*' known-hosts-files 'Users/<username>/other/path/known_hosts'

In hindsight it seems reasonable: to set known-hosts-files to a list of files, do not use quotes at all (~ is then OK to use again).

So the following works:

zstyle ':completion:*:*:ssh:*:*' known-hosts-files /etc/ssh/ssh_known_hosts ~/.ssh/known_hosts ~/other/path/known_hosts

This seems to be related to the fact that the shell is interpreting the quoted text as a literal single string... Naturally...

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.