Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom git command to zsh completion?

I've read a few guides on zsh completion, but I am still confused. In our development environment we have a custom Git command called git new-branch. I'd like zsh to auto-complete it for me after typing just git ne and a Tab. How can I do that?

like image 987
Sergiy Belozorov Avatar asked Aug 02 '16 15:08

Sergiy Belozorov


People also ask

How do I complete a command in zsh?

When you type a command in Zsh and hit the TAB key, a completion is attempted depending on the context. This context includes: What command and options have been already typed at the command-line prompt. Where the cursor is.

How to initialize zsh completion for the current session?

Let’s dive into the fantastic and dangerous Zsh completion system. To initialize the completion for the current session, you need to autoload the function compinit and to call it. To do so, add the following in your file .zshrc:

How do I use zsh-autocomplete with zsh?

The first method we shall use is the zsh-autocomplete repository, a simple shell script that enables real-time auto-completion and suggestions in your ZSH shell as you type. Before installing it, ensure you have ZSH and git installed and up to date.

Where can I find the completers for zsh?

You can find them in the official repository of Zsh. Three folders are specifically important: Base - The core of the completion system, defining the basic completers. Zsh - Functions for completing built-in Zsh commands (like cd for example). Unix - Function for completing external commands.


2 Answers

The default git completion is extendible:

Say you got your own git sub-commands (git will run a program `git-foo' when you run "git foo") and you want "git f" to complete that sub commands name for you. You can make that sub-command known to the completion via the user-command style:

% zstyle ':completion:*:*:git:*' user-commands foo:'description for foo'

`user-commands' is a list style, so you can add any number of programs there. The :description part is optional, so you could add all git-* programs from your $path like this:

% zstyle ':completion:*:*:git:*' user-commands ${${(M)${(k)commands}:#git-*}/git-/}

That is, it suffices to add

zstyle ':completion:*:*:git:*' user-commands new-branch:'custom new branch function'

to your zshrc.

If you would like to handle parameters to your custom command as well, then it is a better solution to use a custom compdef file. The file referenced above has some details on that as well: Just create a standard definition file defining a git-<yourcommand> function, the default git completion will use it automatically when needed.

like image 105
Phillip Avatar answered Oct 24 '22 05:10

Phillip


With Git 2.18 (q2 2018), you have a new possibility, which applies not just for zsh: The command line completion mechanism (in contrib/) has learned to load a custom completion file for "git $command" where $command is a custom "git-$command" that the end user has on the $PATH when using newer version of bash.

See commit 085e2ee (29 Apr 2018) by Florian Gamböck (FloGa).
(Merged by Junio C Hamano -- gitster -- in commit fb3a0ca, 23 May 2018)

completion: load completion file for external subcommand

Adding external subcommands to Git is as easy as to put an executable file git-foo into PATH.
Packaging such subcommands for a Linux distribution can be achieved by unpacking the executable into /usr/bin of the user's system.
Adding system-wide completion scripts for new subcommands, however, can be a bit tricky.

Since bash-completion started to use dynamical loading of completion scripts since v1.90 (preview of v2.0), it is no longer sufficient to drop a completion script of a subcommand into the standard completions path, /usr/share/bash-completion/completions, since this script will not be loaded if called as a git subcommand.

For example, look at https://bugs.gentoo.org/544722.
To give a short summary: The popular git-flow subcommand provides a completion script, which gets installed as /usr/share/bash-completion/completions/git-flow.

If you now type into a Bash shell:

git flow <TAB>

You will not get any completions, because bash-completion only loads completions for git and git has no idea that git-flow is defined in another file.
You have to load this script manually or trigger the dynamic loader with:

`git-flow <TAB>` # Please notice the dash instead of whitespace

This will not complete anything either, because it only defines a Bash function, without generating completions.
But now the correct completion script has been loaded and the first command can use the completions.

So, the goal is now to teach the git completion script to consider the possibility of external completion scripts for subcommands, but of course without breaking current workflows.

How? This is what Git 2.18 proposes:

I think the easiest method is to use a function that was defined by bash-completion v1.90, namely _completion_loader.
It will take care of loading the correct script if present.
Afterwards, the git completion script behaves as usual.

_completion_loader was introduced in commit 20c05b43 of scop/bash-completion (the programmable completion functions for bash) back in 2011, so it should be available in even older LTS distributions.
This function searches for external completion scripts not only in the default path /usr/share/bash-completion/completions, but also in the user's home directory via $XDG_DATA_HOME and in a user specified directory via $BASH_COMPLETION_USER_DIR.


univerio adds in the comments:

It turns out that there are two different completion functions:

  • One ships with zsh, and
  • the other ships with Git.

univerio adds:

  • The zsh-provided function is the default on Debian (and Ubuntu, Mint, etc), and
  • the git-provided function is the default on homebrew-installed git on macOS.

Super confusing. Not sure which one is better.

This particular answer works only for the git-provided function, while the accepted answer works only for the zsh-provided function.

like image 3
VonC Avatar answered Oct 24 '22 05:10

VonC