git-open
git-open copied to clipboard
Implement branch autocomplete
I was trying to find a way of adding an autocomplete script to a subcommand but I couldn't find a way.
Reading the git-completion.bash I noticed that it has a pattern for the function names. For instance _git_stash()
.
So it came to me and I created the function _git_open()
and it works perfectly!
This is a temporary proposal: (this is inside my .profile)
function _git_open()
{
local word=${COMP_WORDS[COMP_CWORD]}
COMPREPLY=($(compgen -W "$(git branch -r)" -- "${word}"))
}
complete -F _git_open git-open
The only downside so far is that it lists only remote
branches.
When testing local branches with COMPREPLY=($(compgen -W "$(git branch | sed)" -- "${word}"))
, for some reason, it was also listing the files and directories.
A working example =)
function _git_open()
{
local word=${COMP_WORDS[COMP_CWORD]}
local local_branches=$(__git_heads)
local remote_branches=$(git branch -r | sed 's/[->]//g')
COMPREPLY=($(compgen -W "$local_branches $remote_branches" -- "${word}"))
}
complete -F _git_open git-open
@eelucaslima this script will work if using bash
but no sh
.
It also depends on git autocomplete script, for both shells.
@gerep, shouldn't the script check the user's shell?