【问题标题】:How to make zsh autocomplete failover to compdef _files如何使 zsh 自动完成故障转移到 compdef _files
【发布时间】:2026-02-07 14:30:01
【问题描述】:

我需要使用以下方法获取 ssh 密钥的指纹:

$ ssh-keygen -lf ~/.ssh/id_rsa_user.pub

输入后:ssh-keygen -lf ~/.ss TAB 没有给出任何选项。

我必须这样做:

$ compdef _files ssh-keygen

之后我可以使用文件路径自动完成,但命令自动完成停止工作,所以如果我输入 ssh-keygen - Tab 我不会再看到这个输出:

$ ssh-keygen -
 -- option --
-B  -- show the bubblebabble digest of key
-C  -- provide new comment
-D  -- download key stored in smartcard reader
-N  -- provide new passphrase
-P  -- provide old passphrase
-U  -- upload key to smartcard reader
-b  -- specify number of bits in key
-c  -- change comment in private and public key files
-e  -- export key to SECSH file format
-f  -- key file
-i  -- import key to OpenSSH format
-l  -- show fingerprint of key file
-p  -- change passphrase of private key file
-q  -- silence ssh-keygen
-t  -- specify the type of the key to create
-y  -- get public key from private key

所以想知道是否有办法同时启用这两个选项,以便我可以执行以下操作:

ssh-keyg TAB 这会给我:

$ ssh-keygen

那我就可以了

$ ssh-keygen - TAB

这将打印选项菜单并可以执行:

$ ssh-keygen -lf ~/.ss TAB

并列出了可行的选项列表,因此最后我会得到类似的结果:

$ ssh-keygen -lf ~/.ssh/id_rsa_user.pub

现在我只是添加到我的~/.zshrc 这个:

compdef _files ssh-keygen
compdef _files adb
compdef _files mysql
...

但我必须为要使用 _files 完成的 avery 命令执行此操作,因此我想知道是否有办法始终使用 _files 或故障转移到它。

有什么想法吗?

在按下 Ctrl+d 显示文件选项时,是否可以让它表现得像 csh

更新:

我注意到如果我的最后一个参数是 -f 自动完成功能:

ssh-keygen -l -f ~/.sshTAB

但是对于自定义脚本,命令什么功能,小部件别名可以帮助强制 _files 完成。?

【问题讨论】:

  • “是否有可能让它像 csh 一样在按 Ctrl+d 时显示文件选项?” - 我认为您可以使用zsh docs for completion widgets中给出的示例来实现这一点
  • 我知道这不一定对您有帮助,但是您使用 ssh-keygen 所追求的正是我的 zsh 的行为方式,所以这绝对是可能的。 ssh-keygen -lf ~/.ssh 给了我一个文件完成列表。
  • @NilsLuxton 嗨,您既可以获取选项ssh-keygen -TAB,也可以在执行ssh-keygen -lt ~/.sTAB 时获取文件,如果可以,请告诉我您是怎么做的。
  • 是的,没错,但我不太了解这一切是如何运作的,无法告诉您如何操作,恐怕……对不起!
  • 如果你在#zsh 回到 IRC,我们可以一起尝试解决问题 - 我发现了一些我认为的东西

标签: autocomplete zsh zsh-completion


【解决方案1】:

解决方案是更新到zsh 我就是这样做的:

# check the zsh info
brew info zsh

# install zsh
brew install zsh

# add shell path
sudo vim /etc/shells

# add the following line into the very end of the file(/etc/shells)
/usr/local/bin/zsh

# change default shell
chsh -s /usr/local/bin/zsh

我在完成 git-flow 时遇到问题,通过使用 --without-completions 重新安装已解决:

$ brew install git --without-completions

【讨论】: