Adam Parkin
Adam Parkin

Reputation: 18690

Autocomplete Git commands like Mercurial does

Not entirely sure how to phrase the question, but Mercurial has a feature I'm quite fond of where when you specify part of a command it's smart enough to recognize which command you mean. So for example: hg sta will be expanded to hg status.

Is there any way to get the equivalent behaviour in Git, so that (for example) a git sta will automatically expand to git status?

Edit: to clarify: I know you can tab-complete with Git. I also know you can create aliases to shorten commands. What I want is like Mercurial if you don't tab complete it infers the command when possible. For example:

git stas

followed by enter be executed as git stash.

Upvotes: 0

Views: 119

Answers (2)

Christophe Muller
Christophe Muller

Reputation: 5090

This Mercurial feature is very convenient indeed because it knows exactly when the number of letters is not enough and so when the command is ambiguous. It also gives you the list of possibilities.

This said, this feature does not exist in git, and it seems that the way it takes is to provide shell-dependent completions, as explained above. If you want to discuss this with the git developers you can still make a request for enhancement on the http://www.mail-archive.com/[email protected]/ mailing list (or see before if it has been asked already).

Upvotes: 1

Eugen Konkov
Eugen Konkov

Reputation: 25153

Just put this script to ~/.git-completion.bash

and add this lines into ~/.bashrc

#added by KES
export LESS="-RFXS -x4"
export TZ='Europe/Zaporozhye'


#Turn off wired ~
printf "\e[?2004l"

if [ -z "$SSH_CLIENT" ]; then
    export EDITOR="subl"
else
    export EDITOR="rsub --port 52697"
    export RMATE_PORT="52697"
fi

alias gn="git-number"
alias ge="gn -c $EDITOR"
alias ga="gn add"
alias gap="EDITOR='$EDITOR -w' gn add -p"
alias gd="gn -c git diff -b -w --ignore-blank-lines"
alias gds="gd --staged"
alias gc="gn -c git checkout"
alias gcf="git flow feature checkout"
alias gl="gn -c git log -w -b -p --ignore-blank-lines"
alias gls="git log --stat"
alias cm="EDITOR='$EDITOR -w' git commit"

gcd() {
    test -n "$1" && cd $(dirname $(git list $1))
}

source ~/.git-completion.bash
__git_complete gn  _git
__git_complete ga  _git_add
__git_complete gap _git_add
__git_complete gd  _git_diff
__git_complete gds _git_diff
__git_complete gc  _git_checkout
__git_complete gcf _git_checkout
__git_complete gl  _git_log
__git_complete gls _git_log
__git_complete cm  _git_commit
#added by KES

You just need the source ~/.git-completion.bash but for convenience I use slightly more

NOTICE: One tab for git sta does not work because there are more completions:

$ git sta
stage    stash    status

Try to press TAB two times, or git stat + TAB

Upvotes: 0

Related Questions