如何“复制” zsh 中其他命令的现有补全
我有一个以主机名作为参数的自定义脚本。我知道我可以轻松复制 ssh 的现有完成,如下所示:
compdef myscript=ssh
但这只能完成第一个参数。有没有一种简单的方法可以为所有参数启用相同的完成?
I have a custom script that takes hostnames as parameters. I know that I can easily copy the existing completion of ssh like this:
compdef myscript=ssh
But that only enables completion of the 1st parameter. Is there an easy way to enable the same completion for all parameters?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不知道有什么简单方法可以启用自定义命令的完成。假设您有一个命令
foo
,带有一堆允许的参数bar
、bas
或baz
,那么补全就很简单了:您可以使用foo bar
、foo bas
或foo baz
。不过,如果它们没有或
,您可以使用这三者的任意组合。当“深度”超过 1 时,情况会变得更糟(
bar
可以接受参数car
、cas
和例如,caz
)。在
zsh
中,我的一般理解是命令的完成在完成函数
中有详细说明。这些函数是特定于应用程序的,因为每个应用程序的参数都是特定于这些应用程序的。例如,tmux
(一个终端多路复用器,类似于screen
,以防您不熟悉)有一个相当复杂的完成函数:这是一个链接。如果您想编写自己的完成函数,可以使用该文档并进行访问。这里有一堆我正在(慢慢地)浏览的链接——它们肯定会告诉你如何完成工作,但是有很多阅读。 Z-Shell 是一个复杂的野兽。
您对启用类似于主机名的参数的完成特别感兴趣,并且您选择了
ssh
作为这个想法。如果您有 ZSH 源代码,则ssh
的zsh
补全函数在Completion/Unix/Command/_ssh
中定义。如果没有,这里有 链接。当然,这个问题是比较相似的。如果
myscript
和ssh
参数足够相同,则compdef
本身就可以完成您想要的操作。I'm not aware of an easy method to enable completion for a custom command. Assuming you've got a command
foo
with a bunch of allowable argumentsbar
,bas
orbaz
, then the completion is easy: you can either havefoo bar
,foo bas
, orfoo baz
. If they're notor
'd, though, you could have any combination of the three.It gets somewhat worse when you've got a 'depth' of more than 1 (
bar
can take argumentscar
,cas
andcaz
, for example).In
zsh
, my general understanding is that completion for commands is detailed incompletion functions
. These functions are application specific, because the arguments for each application are specific to those applications. As an example, thetmux
(a terminal multiplexer, similar toscreen
, in case you're not familiar) has a completion function that's fairly complex: here's a link.If you want to write your own completion functions, the documentation is available and accessible. Here are a bunch of links that I'm (slowly) working my way through - they'll definitely tell you how to get completion working, but there's a lot of reading. The Z-Shell is a complex beast.
You're specifically interested in enabling completion for hostname-like arguments, and you've singled out
ssh
as the idea. Thezsh
completion function forssh
is defined inCompletion/Unix/Command/_ssh
, if you've got the ZSH source. If not, here's a link.Of course, this SO question is rather similar.
compdef
alone may do what you want, ifmyscript
andssh
parameters are identical enough.