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.
What a handsome question. I just had the very same one.
The short answer:
zshtab completion forsshdoesn'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.Debugging it
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
To find the file, my first instinct is to use
type -a. Buttype -a _hostsyields_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:
Oooooookay.
zshtries to complete from~/.ssh/config, this I figured out just be using it. But it looks likezshALSO tries to complete from~/.ssh/known_hosts. Makes sense! I edited that file, and immediately my annoyance was fixed.Additional note on how to understand traces:
There is a shell option that says "Print
$PS4thencommandto$XTRACEFDbefore executing it".set -x, orset -o xtraceare 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 -xNow 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:
I already knew that
-Mand-Jwere 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-MwasX: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.