Especially for git I have found this shortcut function which works better than make-completion-wrapper:
Code:
__define_git_completion () {
eval "
_git_$2_shortcut () {
COMP_LINE=\"git $2\${COMP_LINE#$1}\"
let COMP_POINT+=$((4+${#2}-${#1}))
COMP_WORDS=(git $2 \"\${COMP_WORDS[@]:1}\")
let COMP_CWORD+=1
local cur words cword prev
_get_comp_words_by_ref -n =: cur words cword prev
_git_$2
}
"
}
__git_shortcut () {
type _git_$2_shortcut &>/dev/null || __define_git_completion $1 $2
alias $1="git $2 $3"
complete -o bashdefault -o default -o nospace -F _git_$2_shortcut $1
}
__git_shortcut ga add
__git_shortcut gb branch
__git_shortcut gc commit
__git_shortcut gd diff
__git_shortcut go checkout
__git_shortcut gcp cherry-pick
Source: https://github.com/bronson/dotfiles/...5c/.bashrc#L81
Problem with make-completion-wrapper and git was for "go" as "git checkout" for example:
git checkout <tab> shows a list of all branches.
While go <tab> shows a list of all files in the directory.
I think this is because the make-completion-wrapper function does not set COMP_LINE and COMP_POINT. Also _get_comp_words_by_ref is not called and so "cur words cword prev" are not set which are expected to be set by the _git_* functions.
Maybe the make-completion-wrapper function can be changed accordingly?
//edit
Also have a look at this discussion here:
http://thread.gmane.org/gmane.comp.v...4/focus=185232
//edit2
Edited the function to also work with cherry-pick (the git function is called _git_cerry_pick).
Code:
__define_git_completion () {
# Because cherry-pick has the function _git_cherry_pick
n2=${2//-/_}
eval "
_git_$2_shortcut () {
COMP_LINE=\"git $2\${COMP_LINE#$1}\"
let COMP_POINT+=$((4+${#2}-${#1}))
COMP_WORDS=(git $2 \"\${COMP_WORDS[@]:1}\")
let COMP_CWORD+=1
local cur words cword prev
_get_comp_words_by_ref -n =: cur words cword prev
_git_$n2
}
"
}
//edit3
Ok, so finally I have only one function for creating the wrapper function:
Code:
#!/bin/sh
# Author.: Ole J
# Date...: 23.03.2008
# License: Whatever
# Wraps a completion function
# make-completion-wrapper <actual completion function> <name of new func.> <alias>
# <command name> <list supplied arguments>
# eg.
# alias agi='apt-get install'
# make-completion-wrapper _apt_get _apt_get_install apt-get install
# defines a function called _apt_get_install (that's $2) that will complete
# the 'agi' alias. (complete -F _apt_get_install agi)
#
#make-completion-wrapper _git_checkout _git_checkout_shortcut go git checkout
function make-completion-wrapper () {
local comp_function_name="$1"
local function_name="$2"
local alias_name="$3"
local arg_count=$(($#-4))
shift 3
local args="$*"
local function="
function $function_name {
COMP_LINE=\"$@\${COMP_LINE#$alias_name}\"
let COMP_POINT+=$((${#args}-${#alias_name}))
((COMP_CWORD+=$arg_count))
COMP_WORDS=("$@" \"\${COMP_WORDS[@]:1}\")
local cur words cword prev
_get_comp_words_by_ref -n =: cur words cword prev
"$comp_function_name"
return 0
}"
eval "$function"
# echo $function_name
# echo "$function"
}
__git_shortcut () {
# Because cherry-pick has the function _git_cherry_pick
n2=${2//-/_}
type _git_${n2}_shortcut &>/dev/null || make-completion-wrapper _git_$n2 _git_${n2}_shortcut $1 git $2
complete -o bashdefault -o default -o nospace -F _git_${n2}_shortcut $1
}
__apt_cache_shortcut () {
type _apt_cache_$2_shortcut &>/dev/null || make-completion-wrapper _apt_cache _apt_cache_$2_shortcut $1 apt-cache $2
# is not executed automatically. Normally only on first apt-cache <tab>
[ ! -f /usr/share/bash-completion/completions/apt-cache ] || source /usr/share/bash-completion/completions/apt-cache
complete -F _apt_cache_$2_shortcut $1
}
Saved this as /usr/local/bin/make-completion-wrapper and use it in my $HOME/.bashrc :
Code:
if [ -f /usr/local/bin/make-completion-wrapper ]; then
. /usr/local/bin/make-completion-wrapper
# qhe
make-completion-wrapper _quilt_completion _qhe qhe quilt header -e
complete -o filenames -F _qhe qhe
# apt-policy
__apt_cache_shortcut apt-policy policy
# apt-show
__apt_cache_shortcut apt-show show
# __git_shortcut gs status # no completion for git status
__git_shortcut ga add
__git_shortcut gb branch
__git_shortcut gc commit
__git_shortcut gd diff
__git_shortcut go checkout
__git_shortcut gcp cherry-pick
fi
Bookmarks