4

I currently have a /.ssh/config file the has about 7000 lines in it.

I am trying to see if there is a way to speed up the autocomplete feature in zsh for this.

When I start typing which server I want to ssh into and then press TAB it takes about 10 seconds for it to show me the possible Hosts I can choose from.

Jon Ngo
  • 93
  • 4
  • The unhelpful answer is to redefine the `_ssh_hosts` function found in `/usr/share/zsh/*/functions/_ssh`. It is inherently slow: it reads your file line-by-line, looking for `Host` directives and compiling the results. The biggest problem is that it doesn't cache the host names it finds the first time (which is reasonable for small files, since it means not having to implement logic for detecting when modifications have been made to the file that would invalidate the cache). – chepner Jan 13 '20 at 14:46
  • 1
    This answer gives a way to redefine the completion to be much faster: https://stackoverflow.com/a/64147638/874671 – dshepherd Dec 24 '20 at 12:46

1 Answers1

1

The problem is that the completion for _ssh reads your entire config file line-by-line looking for Host directives each time you try to complete a host.

One solution would be to override the relevant function (_ssh_hosts) to do something faster (like cache host names in memory for subsequent completions).

Another would be to make your config file smaller by moving the actual configuration into separate files, leaving your config file as nothing but a list of Host/Include pairs. For example, turn

Host foo
    User bob
    IdentityFile ~/.ssh/bob_ident

Host bar
    User alice
    IdentityFile ~/.ssh/alice_ident

into

Host foo
    Include foo_config

Host bar
    Include bar_config

where ~/.ssh/foo_config and ~/.ssh/bar_config would contain the relevant details for each host.

Presumably, Host directives only make up a small fraction of your 7000+l lines, so this should speed up _ssh_host considerably.

chepner
  • 497,756
  • 71
  • 530
  • 681
  • Has anyone tried this? Did it make a significant impact? – Chris Jun 23 '22 at 12:52
  • I tested this out and it did not help for me - the includes get pulled in during auto-completion it would seem, because it was just as slow. (SSH 8.4, ZSH 5.8) – Chris Jun 27 '22 at 11:04