zsh:当我键入命令 Y 时,对命令 X 使用补全

发布于 2024-10-03 02:52:38 字数 537 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紫﹏色ふ单纯 2024-10-10 02:52:38

compdef 的文档说明了这一点:

函数compdef可用于将现有完成函数与新命令关联起来。例如,

compdef _pids foo

但是调整它(_gitgit的常见完成功能)并没有为我产生工作结果(即使在_git之后已自动加载):

compdef _git g

我可以通过 _dispatch 让它工作:

compdef '_dispatch git git' g

The documentation for compdef says this:

The function compdef can be used to associate existing completion functions with new commands. For example,

compdef _pids foo

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):

compdef _git g

I was able to get it to work via _dispatch though:

compdef '_dispatch git git' g
七度光 2024-10-10 02:52:38

在更改配置后,相同的功能已停止为我工作。

# in ~/.zsh/functions/g.zsh

# # No arguments: `git status`
# # With arguments: acts like `git`
g() {
  if [[ $# > 0 ]]; then
    git "$@"
  else
    git status
  fi
}

实际上,仅将函数放在 ~/.zsh/functions/g.zsh 中并在 ~/.zsh/completions/_g 中创建一个 compdef 非常重要:

#compdef g
compdef g=git

然后,在.zshrc

fpath=($HOME/.zsh/completions $fpath)

# load custom executable functions
for function in ~/.zsh/functions/*.zsh; do
  source $function
done

# completion
autoload -U compinit
compinit

不确定顺序是否重要。我认为当 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.

# in ~/.zsh/functions/g.zsh

# # No arguments: `git status`
# # With arguments: acts like `git`
g() {
  if [[ $# > 0 ]]; then
    git "$@"
  else
    git status
  fi
}

It's actually important to place just the function in ~/.zsh/functions/g.zsh and create a compdef in ~/.zsh/completions/_g:

#compdef g
compdef g=git

Then, in .zshrc:

fpath=($HOME/.zsh/completions $fpath)

# load custom executable functions
for function in ~/.zsh/functions/*.zsh; do
  source $function
done

# completion
autoload -U compinit
compinit

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文