添加新的 zshell 命令行自定义 git 补全

发布于 2024-12-18 01:11:04 字数 272 浏览 3 评论 0原文

我通过 Homebrew 安装了 git。我通过安装的脚本获得命令行补全,

/usr/local/etc/bash_completion.d/

但是我希望我的自定义 git-* 脚本也能完成。

我如何将其添加到现有的 git 补全中?

I installed git via Homebrew. I get command line completions via the script installed in

/usr/local/etc/bash_completion.d/

However I want my custom git-* scripts to be completed too.

How would I tack this on to existing git completions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

醉梦枕江山 2024-12-25 01:11:04

我给你举几个例子。

  • 为别名添加补全

如果您有一个像这样的 pull 别名:

alias gp='git push'

那么您可以定义别名以使用与 git-push 相同的补全功能。

compdef _git gp=git-push
  • 为新命令添加补全

这是一项比较困难的任务。为 zsh 编写完成脚本并不简单,您可以看一下 在这个项目中寻求一些指导。例如,查看完成脚本 for git-wtf

  • 重用现有的补全,但进行修改

如果您有要在日志中搜索的脚本喜欢this:

query="$1"
shift
git log -S"$query" "$@"

您想要使用 git-log 的补全,并进行一些小修改:您想要首先完成搜索字符串,然后使用 git-log 的常用选项>。然后你可以使用这个:

_git-search () {
if (( CURRENT == 2 )); then
    _message "search string"
    return
fi

CURRENT=$(( $CURRENT - 1 ))
_git-log
}

_git-search "$@"

编辑:此外,要实际使用新定义的完成文件,你必须将存储它们的目录添加到fpath

I'll give you a couple of examples.

  • Adding completion for an alias

If you have an alias for pull like this one:

alias gp='git push'

then you can define the alias to use the same completion as git-push by doing.

compdef _git gp=git-push
  • Adding completion for a new command

This is a tougher one. Writing completion scripts for zsh is not trivial, you can take a look at the ones in this project for some guidance. For example, take a look at the completion script for git-wtf

  • Reuse existing completion, but modified

If you have a script to search in the log like this:

query="$1"
shift
git log -S"$query" "$@"

You want to use the copmletion of git-log, with a small modification: You want to complete first for a search string and then use the usual options for git-log. Then you can use this:

_git-search () {
if (( CURRENT == 2 )); then
    _message "search string"
    return
fi

CURRENT=$(( $CURRENT - 1 ))
_git-log
}

_git-search "$@"

EDIT: Also, to actually use your newly defined completion files, you have to add the directory where they are stored to the fpath

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