zsh:当我键入命令 Y 时,对命令 X 使用补全
在 zsh 中,我有一个名为 g
的函数,其作用如下:
- 不带参数,
- 使用一个或多个参数调用
git status
,使用所有给定参数委托给 git - 即调用git $@
我希望 g
的制表符补全与 git
完全相同。我可以使用 alias g=git
来实现此目的,但这不允许我默认调用 status
(上面的第一点)。
我如何委托完成 git
?
在 bash 中,我只是执行了 complete -F _git g
,它重新使用了 git 的完成功能。使用 zsh,git 的补全看起来要复杂得多,我无法找到类似的解决方案。
我猜想 zsh 中有一些函数可以说“假装我输入了命令 [x],你会把它完成什么?”。如果我知道那是什么,那么使用函数来委托它应该足够简单。但我在手册中没有找到这样的功能。
In zsh, I have a function called g
which acts like this:
- with no arguments, call
git status
- with one or more arguments, delegate to git with all given arguments - i.e. call
git $@
I would like the tab completions for g
to be exactly the same as for git
. I can achieve this with alias g=git
, but that doesn't allow me to call status
by default (the first point above).
How can I delegate to the completion for git
?
In bash, I simply did complete -F _git g
which re-uses git's completion function. With zsh, git's completion looks much more complex, and I wan't able to find a similar solution.
I'd guess there's some function in zsh to say "pretend I typed command [x], what would you complete it to?". If I knew what that was, it should be simple enough to use a function to delegate to it. But I've found no such function in the manuals.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
compdef
的文档说明了这一点:但是调整它(
_git
是git的常见完成功能)并没有为我产生工作结果(即使在_git
之后已自动加载):我可以通过 _dispatch 让它工作:
The documentation for
compdef
says this:But adapting it (
_git
is the usual completion function for git) did not produce a working result for me (even after_git
had been autoloaded):I was able to get it to work via
_dispatch
though:在更改配置后,相同的功能已停止为我工作。
实际上,仅将函数放在
~/.zsh/functions/g.zsh
中并在~/.zsh/completions/_g
中创建一个 compdef 非常重要:然后,在
.zshrc
:不确定顺序是否重要。我认为当 compdef 位于单独的文件夹中时,它适用于任何顺序。
从这里获取
g
函数:https: //github.com/thoughtbot/dotfiles/blob/master/zsh/functions/g
https://github.com/thoughtbot/dotfiles/blob/master/zsh/completion/_g
谢谢 Thoughtbot!
This same function had stopped working for me after a change-around of configs.
It's actually important to place just the function in
~/.zsh/functions/g.zsh
and create a compdef in~/.zsh/completions/_g
:Then, in
.zshrc
:Not sure if the order is important. I think when the compdef is in a separate folder, it works with any order.
Got the
g
function from here:https://github.com/thoughtbot/dotfiles/blob/master/zsh/functions/g
https://github.com/thoughtbot/dotfiles/blob/master/zsh/completion/_g
Thanks Thoughtbot!