Surya
Surya

Reputation: 4972

adding new zshell command line custom git completions

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?

Upvotes: 4

Views: 635

Answers (1)

Mario F
Mario F

Reputation: 47279

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

Upvotes: 3

Related Questions