Reputation: 4723
I was trying to create aliases like this:
alias '.a'='git add'
alias '.d'='git diff'
alias '.p'='git push'
alias '.f'='git fetch'
alias '.o'='git checkout'
alias '.c'='git commit -m'
alias '.b'='git branch'
alias '.s'='git status'
alias '.m'='git merge'
alias '.l'='git log -n 20 --oneline'
Autocompletion is always important, so I tried like this:
complete -F _git_checkout .o
But I got errors like after typing <tab><tab>
:
#➤➤ .o bash: [: 1: unary operator expected
bash: [: 1: unary operator expected
Then how can I make it work here?
My desktop runs on Ubuntu 13.04
@1, add a piece of code here where it seems failed on Ubuntu:
➤➤ complete | ack-grep alias
complete -F _alias_completion::grep grep
complete -F _alias_completion::la la
complete -F _alias_completion::ll ll
complete -F _alias_completion::l l
complete -F _alias_completion::ls ls
complete -a unalias
Upvotes: 1
Views: 951
Reputation: 175
I've been looking for an auto-completion solution specifically for a "git checkout" alias (gc, which is probably not a good alias) and came up with defining modifies the $COMP_WORDS and $COMP_CWORD variables:
# !/bin/sh
# Define a custom complete function for 'git checkout'.
___git_gco ()
{
# Set the first word to git and insert checkout.
COMP_WORDS=(git checkout ${COMP_WORDS[@]:1})
# Increment the current word cursor, since we added one word.
COMP_CWORD=$((COMP_CWORD + 1))
# Show some debug information.
# echo -n -e "\n${COMP_WORDS[0]}"
# echo -n -e "\n${COMP_WORDS[1]}"
# echo -n -e "\n${COMP_WORDS[$COMP_CWORD]}"
# echo -n -e "\n${COMP_WORDS[@]}"
# Call Git's default complete function.
_git
}
# Define an alias for 'git checkout'.
alias gco="git checkout"
# Set the new alias to use the custom complete function.
complete -o bashdefault -o default -o nospace -F ___git_gco gco
I don't know how good this solution is since it's messing with prepared by bash (there could be other variables supplied that I'm unaware of which might need modifications). It was tested only for the simplest case of checking out a branch without any options (although options do get completed).
Bottom line is - works for checking out a branch.
Upvotes: 1
Reputation: 19395
Do not define completions like complete -F _git_checkout .o
.
Use the alias completion code (pointed to by Ansgar Wiechers above). After sourcing that script, completions like
complete -o bashdefault -o default -o nospace -F _alias_completion::.o .o
have been defined.
Upvotes: 0