Skip to content

Instantly share code, notes, and snippets.

@aminland
Last active August 14, 2018 20:31
Show Gist options
  • Save aminland/6907424ff10b4d2e95016c0c13d1f85a to your computer and use it in GitHub Desktop.
Save aminland/6907424ff10b4d2e95016c0c13d1f85a to your computer and use it in GitHub Desktop.
bash alias completion
alias_completion(){
# keep global namespace clean
local cmd completion
# determine first word of alias definition
# NOTE: This is really dirty. Is it possible to use
# readline's shell-expand-line or alias-expand-line?
cmd=$(alias "$1" | sed 's/^alias .*='\''//;s/\( .\+\|'\''\)//')
# determine completion function
completion=$(complete -p "$cmd" 2>/dev/null)
echo $completion;
[[ -n $completion ]] || echo 'test';
# run _completion_loader only if necessary
[[ -n $completion ]] || {
# load completion
_completion_loader "$cmd"
# detect completion
completion=$(complete -p "$cmd" 2>/dev/null)
}
# ensure completion was detected
[[ -n $completion ]] || return 1
# configure completion
eval "$(sed "s/$cmd\$/$1/" <<<"$completion")"
}
# set aliases
alias dc='docker-compose'
alias vbox=VBoxManage
# aliases to load completion for
aliases=(dc vbox)
for a in "${aliases[@]}"; do
alias_completion "$a"
done
# clean up after ourselves
unset a aliases
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment