I was reading How do I get bash completion for command aliases? and I thought, that the answer by Shawn J. Goff needed a little something: less user setup so I wrote a little script that is intended to be sourced from your .bashrc
, but it doesn't quite work, so hopefully someone can spot my error.
function make-completion-wrapper-1 () {
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$(complete -pr $1|sed -e 's/^\(.* -F \)\([^ ]*\) [^ ]*$/\2/')"
shift 2
local function="
function $function_name {
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" \${COMP_WORDS[@]:1} )
"$comp_function_name"
return 0
}"
echo "$function"
eval "$function"
}
function make-completion-wrapper-2 () {
local function_name="$1"
local alias_name="$2"
local comp_function_base="$(complete -pr $3|sed -e 's/^\(.* -F\)\( [^ ]*\) [^ ]*$/\1/')"
echo "$comp_function_base $function_name $alias_name"
eval "$comp_function_base $function_name $alias_name"
}
[ -e ~/.cache/bash_alias_complete ] && rm ~/.cache/bash_alias_complete
alias | \
sed \
-e 's/^alias //' \
-e '/^\([^=]\+\)='\''\1 /d' \
-e "/'.*=/d" -e '/\$/d' \
-e h \
-e "s/.*='\([^ ]*\)\( .*\)'$/\1! \1\2/" \
-e 'y/ -!/__ /' -eG -e 's!\n[^=]*='\''\(.*\)'\''! \1!' \
-e 's/^/make-completion-wrapper-1 /p' \
-e 's/^[^ ]* [^ ]* \([^ ]*\) .*$/\1/' \
-e x \
-e 's!='\''\([^ ]*\) .*$! \1!' \
-e x \
-e 's!^!make-completion-wrapper-2 !' \
-e G \
-e 'y/\n/ /' \
> ~/.cache/bash_alias_complete
. ~/.cache/bash_alias_complete
I'm getting the following error when I pres the Tab key:
bash: COMP_POINT - : syntax error: operand expected (error token is "- ")
NOTE: The above code can be placed in a file, such as myfuncs.bash
, which can then either be sourced directly in the shell or from $HOME/.bashrc
.
For full change history on this code see https://github.com/hildred/scripts.git