#669618 bash-completion should autocomplete bash alias as well

#669618#5
Date:
2012-04-20 12:59:57 UTC
From:
To:
i think is wanted and feasible to make bash-completion to autocomplete
something like

alias aptinstall='apt-get install'

just in the same way it would autocomplete in bash shell after typing those
words

either buildin alias autocompletion (most awesome) or some stanza or means to
easier to the task of autocomplete alias (less awesome but still useful).

Thanks!

#669618#10
Date:
2013-10-09 23:09:25 UTC
From:
To:
A related problem is that aliasing an existing command stops
bash-completion working. For example, I had:

alias make=colormake

And that stops bash-completion from working for either make or
colormake. The code that gets bash-completion working for aliases,
e.g.:

http://superuser.com/questions/436314/how-can-i-get-bash-to-perform-tab-completion-for-my-aliases

does not work for this case (where the name of a command with bash
completions is aliased to some other command).

This is a pity, as it means that for make I'm forced to choose between
coloring and bash-completion.

#669618#15
Date:
2014-07-19 11:48:17 UTC
From:
To:
I just stumbled on the same problem and after some experimentation, I'm
sourcing a modified completion loader at the end of my .bashrc (specifically
after the original completion code is loaded).

_custom_completion_loader()
{
    local cmd="${1##*/}"
    local cmdt=$(type -t "$1")
    local compfile=./completions

    #[[ $BASH_SOURCE == */* ]] && compfile="${BASH_SOURCE%/*}/completions"
    compfile="/usr/share/bash-completion/completions"

    if [[ $cmdt = 'alias' ]]; then
      cmd=$( alias "$cmd" | sed "s/^alias [^=]*='\([^ ']*\)[ '].*$/\1/" )
      [[ $(type -t "_${cmd}") = 'function' ]] && complete -F "_${cmd}" "$1" &&
return 124
    fi
    compfile+="/${cmd}"

    # Avoid trying to source dirs; https://bugzilla.redhat.com/903540
    [[ -f "$compfile" ]] && . "$compfile" &>/dev/null && return 124

    # Need to define *something*, otherwise there will be no completion at
all.
    complete -F _minimal "$cmd" && return 124
}
complete -D -F _custom_completion_loader


Beware of wrapped lines. This code is fresh from my keyboard and not at all
well-tested!

Michael

#669618#20
Date:
2014-09-25 09:24:13 UTC
From:
To:
This feature will be very useful!

michael your script not work for me :-/

#669618#25
Date:
2014-09-27 08:07:53 UTC
From:
To:
Some of the time it hasn't been working for me, either. Right now (on a
current sid) it appears to work. I'm not really sure why.

In your case, I'm sorry to say, you'll have to do some debugging
yourself.