Reputation: 911
I have some hosts defined in my ~/.ssh/config
file (ari-1
, ari-2
, etc.), but when I type ssh ar<TAB>
I get ssh ari.sweedler.com
.
Granted, that is a valid domain, but it doesn't show up anywhere in my ~/.ssh/config
! I want to debug this. I want to understand what's happening, so I can fix this.
Upvotes: 1
Views: 584
Reputation: 911
What a handsome question. I just had the very same one.
zsh
tab completion for ssh
doesn't just look in ~/.ssh/config
, but also in ~/.ssh/known_hosts
. Remove the host from there if you don't want it as a completion option.
The first step is to know what code is running when you invoke tab completion. That is very easy to do, there is a function: _complete_debug
, which will give you a trace. (see end note for advice on learning to read these)
Next, search through the output to see where the offending piece of text comes from. For me, that was
+_hosts:74> _hosts=( localhost broadcasthost github.com xxx.yyy.io other.website.com ari.sweedler.com )
To find the file, my first instinct is to use type -a
. But type -a _hosts
yields _hosts is an autoload shell function
. Well I asked ChatGPT (before I turned to Google...) and it gave me some paths to check out. The prefix of one of those paths actually existed! /usr/share/zsh/
. Then I searched filenames and file contents for _hosts
, which I found: /usr/share/zsh/5.8.1/functions/_hosts
.
From there, I read the script, until I got to the following line:
khostfiles=(/etc/ssh/ssh_known_hosts ~/.ssh/known_hosts)
Oooooookay. zsh
tries to complete from ~/.ssh/config
, this I figured out just be using it. But it looks like zsh
ALSO tries to complete from ~/.ssh/known_hosts
. Makes sense! I edited that file, and immediately my annoyance was fixed.
There is a shell option that says "Print $PS4
then command
to $XTRACEFD
before executing it". set -x
, or set -o xtrace
are 2 ways to turn this tracing system on. Read about all of these in the bash manual or anywhere else online to understand more. Here's one link that talks about: set -x
Now that you know the grammar of the trace, you may not always know the grammar of the commands being used. Well, your first instinct when looking through traces should be: look everything up in the manual.
Here's a rule of thumb: The older the program is, the more mature and better the documentation is. You'll quickly gain intuition about how to find information in manuals or where to look if you can't find the answer there. Recently, ChatGPT has become incredibly helpful to my workflow - especially when there's a grammar I am failing to parse. ChatGPT can be wrong, but it can also give you leads with where to look in the manual, or what to search for.
For example, I saw this, and had no idea what it did:
_hosts -M 'm:{[:lower:]}={[:upper:]} r:|[._-]=** r:|=**' -J -default-
I already knew that -M
and -J
were options with 1 argument each, but I didn't understand the arguments. ChatGPT was able to break that down for me, stating that the grammar for -M
was X:STRING1=STRING2
. Once I knew that that was a rule, everything got a LOT simpler. I was easily able to get to the right part of the manual: Completion-Matching-Control.
Upvotes: 1