How to enable git file tab completion with zsh compinit?

醉酒当歌 提交于 2021-02-17 19:12:05

问题


I have a problem with the zsh tab completion: After running:

autoload -U compinit
compinit

Git tab completion for files does not work any more. For example if I type git add my_f to complete my_file, nothing happens. The zsh git completion only seems to work for git branches and tags.

Without the compinit stuff, git file completion works, but of course I'm missing out all the fancy branch completion stuff.

So... Is there a way to make git file completion AND git branch completion possible at the same time in zsh? This would really help me a lot.


回答1:


(I'm adding an answer here mostly to get it off the unanswered list.)

As you can see from the comments, it does work. I believe the whole zsh situation has recently improved significantly, too:

commit 06f44c3cc5ca5eca638f300a518c65aa98d26d6d

Date: Mon Sep 6 08:33:19 2010 -0400

completion: make compatible with zsh

Modify git-completion.bash so that it also works with zsh when using bashcompinit. ...

So, if you happen across this googling, and your completion isn't working, try upgrading your git-completion.bash (link points to the blob from git.git HEAD). Current git version is 1.7.3.2; it should make it into the next one.




回答2:


Note that, with Git 2.30 (Q1 2021), the instructions for command line completion (in contrib/) for zsh have been updated.

See commit 3ebd945 (02 Jul 2020) by Alexey (ogonkov).
(Merged by Junio C Hamano -- gitster -- in commit bbdbe08, 02 Nov 2020)

completion: fix zsh installation instructions

Signed-off-by: Alexey
Reviewed-by: Stefan Haller

  • Fix wrong script in completion configuration. zsh wants bash completion path here, not path to itself.
  • Add compinit autoload command, since whole thing didn't work if it is not loaded.

So the command would be:

# The recommended way to install this script is to make a copy of it in
# ~/.zsh/ directory as ~/.zsh/git-completion.zsh and then add the following
# to your ~/.zshrc file:
#
#  fpath=(~/.zsh $fpath)
#  autoload -Uz compinit && compinit

Warning: With Git 2.30 (Q1 2021), Zsh autocompletion (in contrib/) has been patched, as mentioned in here by its author "Felipe Contreras".

See commit af806a2, commit 441ecda, commit 8030684, commit 5a364d2, commit 35a4170, commit bbd7f45, commit 2769e56, commit 9d76052, commit 9a397ea, commit 46af9b3, commit 5d64fb2, commit cf6ce01, commit 35e29fb, commit ecaf798, commit 2f459b0, commit 94b2901, commit a7804a1, commit 3791968, commit aa1f1f8, commit 162f1a5, commit bed6356, commit 81f717b, commit ea625a3, commit 98fc268 (27 Oct 2020), and commit 44b37ab, commit cb9dcbd, commit 2c7cdc5, commit 5eb25bc, commit 4ce2b51 (24 Oct 2020) by Felipe Contreras (felipec).
(Merged by Junio C Hamano -- gitster -- in commit 4560eae, 09 Nov 2020)

completion: bash: remove zsh wrapper

Signed-off-by: Felipe Contreras

It has been deprecated for more than eight years now, it's never up to date, and it's a hassle to maintain.

It's time to move on.


With Git 2.30 (Q1 2021), there is a hotfix for a recent breakage.

See commit 0764964 (05 Dec 2020), and commit c5dd0c8 (30 Nov 2020) by Felipe Contreras (felipec).
(Merged by Junio C Hamano -- gitster -- in commit 7d92da3, 08 Dec 2020)
Cc: SZEDER GáborSigned-off-by: Felipe ContrerasSigned-off-by: Junio C Hamano

completion: bash: fix gitk alias regression

Long time ago when the _git_complete helper was introduced, _gitk was replaced with __gitk_main, and a placeholder for backwards compatibility pointing to __git_wrap_main_gitk was left in place.

When "__git_complete gitk __gitk_main" was called, that created the __git_wrap__gitk_main helper, which is just basically "__git_func_wrap __gitk_main" plus complete options.

Unfortunately the commit b0a4b2d257 ("completion: add support for backwards compatibility", 2012-05-19, Git v1.7.11-rc0 -- merge) missed a previous instance of a call to _gitk in _git_gitk

So, basically we had __git_wrap__git_main -> __git_func_wrap __git_main -> __git_complete_command gitk -> _git_gitk -> _gitk -> __git_wrap__gitk_main -> __git_func_wrap __gitk_main -> __gitk_main.

There was never any need to call __git_func_wrap twice. Since _git_gitk is always called inside the wrapper, it can call __gitk_main directly.

And then, in commit 441ecdab37 ("completion: bash: remove old compat wrappers", 2020-10-27, Git v2.30.0 -- merge listed in batch #4) _gitk was removed, which triggers the following error:

_git_gitk:9: command not found: _gitk  

Let's call the correct function: __gitk_main.



来源:https://stackoverflow.com/questions/1642881/how-to-enable-git-file-tab-completion-with-zsh-compinit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!