How do I get bash completion for command aliases?Forward Autocorrect to a scriptBash tab completion for...

How far off did Apollo 11 land?

What does Windows' "Tuning up Application Start" do?

Inside Out and Back to Front

TCP connections hang during handshake

P-adic functions on annuli

Find position equal columns of matrix

A bicolour masyu

Could Europeans in Europe demand protection under UN Declaration on the Rights of Indigenous Peoples?

Book in which the "mountain" in the distance was a hole in the flat world

Why should fork() have been designed to return a file descriptor?

Can a creature sustain itself by eating its own severed body parts?

Grease/lubricate rubber stabilizer bar bushings?

How deep is the Underdark? What is its max and median depth?

What is this light passenger prop airplane which crash landed in East Kalimantan, Borneo in 1983?

What's a German word for »Sandbagger«?

Why do we need an estimator to be consistent?

What is the minimum wait before I may I re-enter the USA after a 90 day visit on the Visa B-2 Program?

ESTA Travel not Authorized. Accepted twice before!

Is there an English word to describe when a sound "protrudes"?

1025th term of the given sequence.

Nilpotent elements of Lie algebra and unipotent groups

Why didn't Balak request Bilam to bless his own people?

RedirectTo deleting google 360 parameters from URL in Journey builder email sends

You have no, but can try for yes



How do I get bash completion for command aliases?


Forward Autocorrect to a scriptBash tab completion for aliasesHow do you make an alias or function that retains tab completion?code review: automatic completion for bash aliasesApt-get autocompletionTab completion doesn't work for arguments when command is an aliasBash completion throwing syntax errorTab auto completion doesn't work with apt-get install (kubuntu)How to use bash's complete or compgen -C (command) option?Bash completion is very incomplete on centos 7Bash filename completion explanationCustom bash tab completion showing possible completions, but not actually completing inputAre there system settings that affect command line completion?How to enable tab auto completion linux subsystem windows 10






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







51















I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :



alias apt-inst='sudo aptitude install'


Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.










share|improve this question




















  • 1





    See also stackoverflow.com/questions/342969/…

    – nschum
    Aug 17 '12 at 9:17


















51















I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :



alias apt-inst='sudo aptitude install'


Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.










share|improve this question




















  • 1





    See also stackoverflow.com/questions/342969/…

    – nschum
    Aug 17 '12 at 9:17














51












51








51


25






I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :



alias apt-inst='sudo aptitude install'


Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.










share|improve this question
















I am looking to get tab-completion on my command line aliases, for example, say I defined the following alias :



alias apt-inst='sudo aptitude install'


Is there a way to get the completions provided by aptitude when I hit the tab key? i.e. when I write 'sudo aptitude install gnumer' and hit tab, aptitude completes this to gnumeric, or if there was uncertainty lists all the available packages starting with gnumer. If I do it using my alias, nothing - no completion.







bash alias autocomplete






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 '10 at 22:27









Gilles

565k134 gold badges1161 silver badges1670 bronze badges




565k134 gold badges1161 silver badges1670 bronze badges










asked Nov 20 '10 at 16:13









levesquelevesque

1,2724 gold badges13 silver badges14 bronze badges




1,2724 gold badges13 silver badges14 bronze badges








  • 1





    See also stackoverflow.com/questions/342969/…

    – nschum
    Aug 17 '12 at 9:17














  • 1





    See also stackoverflow.com/questions/342969/…

    – nschum
    Aug 17 '12 at 9:17








1




1





See also stackoverflow.com/questions/342969/…

– nschum
Aug 17 '12 at 9:17





See also stackoverflow.com/questions/342969/…

– nschum
Aug 17 '12 at 9:17










5 Answers
5






active

oldest

votes


















29














There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:



function make-completion-wrapper () {
local function_name="$2"
local arg_count=$(($#-3))
local comp_function_name="$1"
shift 2
local function="
function $function_name {
((COMP_CWORD+=$arg_count))
COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
"$comp_function_name"
return 0
}"
eval "$function"
echo $function_name
echo "$function"
}


Use it to define a completion function for your alias, then specify that function as a completer for the alias:



make-completion-wrapper _apt_get _apt_get_install apt-get install
complete -F _apt_get_install apt-inst


I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep, I always want to skip devices and binary files, so I make an alias for grep. For adding new commands such as grepbin, I use a shell script in my ~/bin folder. If that folder is in your path, it will get autocompleted.






share|improve this answer





















  • 2





    Awesome, I was afraid it wouldn't be possible.

    – levesque
    Nov 20 '10 at 17:10











  • Note this code has some issues. See my answer for their explanation and resolution.

    – Tom Hale
    Sep 15 '16 at 13:06











  • Note: I was using this technique for another command, and I had to use complete -o default -F ... instead of complete -F ... to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).

    – joelittlejohn
    Dec 19 '16 at 14:39





















25














Try complete-alias, which solves this problem exactly.



After install it you can use one generic function to complete many aliases like this:



complete -F _complete_alias <myalias1>
complete -F _complete_alias <myalias2>
complete -F _complete_alias <myalias3>




You may want to source the complete_alias file in every bash instance through .bash_profile or similar.



installation



mkdir ~/.bash_completion.d
curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
> ~/.bash_completion.d/complete_alias


application



source ~/.bash_completion.d/complete_alias

alias container=docker container
complete -F _complete_alias container


container can now be autocompleted by the original _docker() completion handler;



$ container l<Tab>
logs ls

$ container s<Tab>
start stats stop





share|improve this answer





















  • 2





    Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.

    – muru
    Dec 24 '16 at 7:55











  • @muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.

    – Cyker
    Dec 25 '16 at 12:12






  • 2





    the point is to be able to judge whether visiting said README is worthwhile, just from the answer.

    – muru
    Dec 25 '16 at 12:14











  • This is an adequate answer and it solves the problem exactly.

    – Ярослав Рахматуллин
    Oct 10 '18 at 23:16






  • 1





    This is an excellent and very comprehensive answer that works perfectly, thanks!. It's probably been updated since @muru had problems with it.

    – Brian Topping
    Oct 19 '18 at 20:21





















3














2018 answer



You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.



For package autocompletion:



complete -c name_of_your_alias


For command autocompletion:



complete -F name_of_your_alias


To check if your alias was added correctly:



complete | grep name_of_your_alias


Finally, to remove an alias from 'complete':



complete -r name_of_your_alias


In your case:



complete -c apt-inst





share|improve this answer





















  • 1





    Please don’t continue your answer in comments. If you have things to say, edit them into the answer.

    – G-Man
    May 21 '18 at 3:02






  • 2





    complete -F xxx does not work on OSX (It just shows the usage complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]) . Do you have any pointer? Thanks.

    – Anthony Kong
    Feb 28 at 22:20











  • Use this instead: unix.stackexchange.com/questions/224227/…

    – Rich
    Jun 5 at 8:20



















2














Here's the code from Shawn J. Goff's answer with some improvements:




  • Fixed syntax errors highlighted by shell-check, eg the first " of "$@" actually ended the function definition string.

  • Removed the return 0 so that the return value of the underlying function can be passed back to the caller.


.



# Wraps a completion function, eg for use with an alias.
# Usage:
# make-completion-wrapper <actual completion function> <name of new func.>
# <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
function make-completion-wrapper {
local function_name="$2"
local arg_count=$(( $#-3 ))
local comp_function_name="$1"
shift 2
local function="function $function_name {
(( COMP_CWORD += $arg_count ))
COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
"$comp_function_name"
}"
eval "$function"
# echo "$function"
}
export -f make-completion-wrapper





share|improve this answer


























  • Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.

    – joelittlejohn
    Dec 19 '16 at 14:38






  • 1





    "Ending" the function definition string is on purpose; the point is to include arguments from the outer function into the COMP_WORDS array. If we use make-completion-wrapper _apt_get _apt_get_install apt-get install as an example, after shift 2, $@ contains apt-get install. The function definition includes apt-get install (the two literal words) when assigning COMP_WORDS.

    – Jeffery To
    Jun 3 at 19:58



















0














As an extension to @Giles answer, the following convenience function auto-names the wrapper generated by make-completion-wrapper to make it possible to define completion in one line:



function complete-alias  {
# uses make-completion-wrapper: https://unix.stackexchange.com/a/4220/50978
# example usage
# complete-alias _pass pshow pass show
# complete-alias _pass pgen pass generate

EXISTING_COMPLETION_FN=${1} && shift
ALIAS=${1} && shift
AUTOGEN_COMPLETION_FN="__autogen_completion_${ALIAS}"
make-completion-wrapper ${EXISTING_COMPLETION_FN} ${AUTOGEN_COMPLETION_FN} ${*}
complete -F ${AUTOGEN_COMPLETION_FN} ${ALIAS}
}





share|improve this answer


























    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "106"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f4219%2fhow-do-i-get-bash-completion-for-command-aliases%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    5 Answers
    5






    active

    oldest

    votes








    5 Answers
    5






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    29














    There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:



    function make-completion-wrapper () {
    local function_name="$2"
    local arg_count=$(($#-3))
    local comp_function_name="$1"
    shift 2
    local function="
    function $function_name {
    ((COMP_CWORD+=$arg_count))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    return 0
    }"
    eval "$function"
    echo $function_name
    echo "$function"
    }


    Use it to define a completion function for your alias, then specify that function as a completer for the alias:



    make-completion-wrapper _apt_get _apt_get_install apt-get install
    complete -F _apt_get_install apt-inst


    I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep, I always want to skip devices and binary files, so I make an alias for grep. For adding new commands such as grepbin, I use a shell script in my ~/bin folder. If that folder is in your path, it will get autocompleted.






    share|improve this answer





















    • 2





      Awesome, I was afraid it wouldn't be possible.

      – levesque
      Nov 20 '10 at 17:10











    • Note this code has some issues. See my answer for their explanation and resolution.

      – Tom Hale
      Sep 15 '16 at 13:06











    • Note: I was using this technique for another command, and I had to use complete -o default -F ... instead of complete -F ... to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).

      – joelittlejohn
      Dec 19 '16 at 14:39


















    29














    There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:



    function make-completion-wrapper () {
    local function_name="$2"
    local arg_count=$(($#-3))
    local comp_function_name="$1"
    shift 2
    local function="
    function $function_name {
    ((COMP_CWORD+=$arg_count))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    return 0
    }"
    eval "$function"
    echo $function_name
    echo "$function"
    }


    Use it to define a completion function for your alias, then specify that function as a completer for the alias:



    make-completion-wrapper _apt_get _apt_get_install apt-get install
    complete -F _apt_get_install apt-inst


    I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep, I always want to skip devices and binary files, so I make an alias for grep. For adding new commands such as grepbin, I use a shell script in my ~/bin folder. If that folder is in your path, it will get autocompleted.






    share|improve this answer





















    • 2





      Awesome, I was afraid it wouldn't be possible.

      – levesque
      Nov 20 '10 at 17:10











    • Note this code has some issues. See my answer for their explanation and resolution.

      – Tom Hale
      Sep 15 '16 at 13:06











    • Note: I was using this technique for another command, and I had to use complete -o default -F ... instead of complete -F ... to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).

      – joelittlejohn
      Dec 19 '16 at 14:39
















    29












    29








    29







    There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:



    function make-completion-wrapper () {
    local function_name="$2"
    local arg_count=$(($#-3))
    local comp_function_name="$1"
    shift 2
    local function="
    function $function_name {
    ((COMP_CWORD+=$arg_count))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    return 0
    }"
    eval "$function"
    echo $function_name
    echo "$function"
    }


    Use it to define a completion function for your alias, then specify that function as a completer for the alias:



    make-completion-wrapper _apt_get _apt_get_install apt-get install
    complete -F _apt_get_install apt-inst


    I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep, I always want to skip devices and binary files, so I make an alias for grep. For adding new commands such as grepbin, I use a shell script in my ~/bin folder. If that folder is in your path, it will get autocompleted.






    share|improve this answer















    There is a great thread about this on the Ubuntu forums. Ole J proposes the following alias completion definition function:



    function make-completion-wrapper () {
    local function_name="$2"
    local arg_count=$(($#-3))
    local comp_function_name="$1"
    shift 2
    local function="
    function $function_name {
    ((COMP_CWORD+=$arg_count))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    return 0
    }"
    eval "$function"
    echo $function_name
    echo "$function"
    }


    Use it to define a completion function for your alias, then specify that function as a completer for the alias:



    make-completion-wrapper _apt_get _apt_get_install apt-get install
    complete -F _apt_get_install apt-inst


    I prefer to use aliases for adding always-used arguments to existing programs. For instance, with grep, I always want to skip devices and binary files, so I make an alias for grep. For adding new commands such as grepbin, I use a shell script in my ~/bin folder. If that folder is in your path, it will get autocompleted.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 28 '12 at 0:16









    Gilles

    565k134 gold badges1161 silver badges1670 bronze badges




    565k134 gold badges1161 silver badges1670 bronze badges










    answered Nov 20 '10 at 16:48









    Shawn J. GoffShawn J. Goff

    31.1k19 gold badges114 silver badges134 bronze badges




    31.1k19 gold badges114 silver badges134 bronze badges








    • 2





      Awesome, I was afraid it wouldn't be possible.

      – levesque
      Nov 20 '10 at 17:10











    • Note this code has some issues. See my answer for their explanation and resolution.

      – Tom Hale
      Sep 15 '16 at 13:06











    • Note: I was using this technique for another command, and I had to use complete -o default -F ... instead of complete -F ... to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).

      – joelittlejohn
      Dec 19 '16 at 14:39
















    • 2





      Awesome, I was afraid it wouldn't be possible.

      – levesque
      Nov 20 '10 at 17:10











    • Note this code has some issues. See my answer for their explanation and resolution.

      – Tom Hale
      Sep 15 '16 at 13:06











    • Note: I was using this technique for another command, and I had to use complete -o default -F ... instead of complete -F ... to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).

      – joelittlejohn
      Dec 19 '16 at 14:39










    2




    2





    Awesome, I was afraid it wouldn't be possible.

    – levesque
    Nov 20 '10 at 17:10





    Awesome, I was afraid it wouldn't be possible.

    – levesque
    Nov 20 '10 at 17:10













    Note this code has some issues. See my answer for their explanation and resolution.

    – Tom Hale
    Sep 15 '16 at 13:06





    Note this code has some issues. See my answer for their explanation and resolution.

    – Tom Hale
    Sep 15 '16 at 13:06













    Note: I was using this technique for another command, and I had to use complete -o default -F ... instead of complete -F ... to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).

    – joelittlejohn
    Dec 19 '16 at 14:39







    Note: I was using this technique for another command, and I had to use complete -o default -F ... instead of complete -F ... to get things like filename auto-completion working correctly when passing args (Bash 4.3.46).

    – joelittlejohn
    Dec 19 '16 at 14:39















    25














    Try complete-alias, which solves this problem exactly.



    After install it you can use one generic function to complete many aliases like this:



    complete -F _complete_alias <myalias1>
    complete -F _complete_alias <myalias2>
    complete -F _complete_alias <myalias3>




    You may want to source the complete_alias file in every bash instance through .bash_profile or similar.



    installation



    mkdir ~/.bash_completion.d
    curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
    > ~/.bash_completion.d/complete_alias


    application



    source ~/.bash_completion.d/complete_alias

    alias container=docker container
    complete -F _complete_alias container


    container can now be autocompleted by the original _docker() completion handler;



    $ container l<Tab>
    logs ls

    $ container s<Tab>
    start stats stop





    share|improve this answer





















    • 2





      Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.

      – muru
      Dec 24 '16 at 7:55











    • @muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.

      – Cyker
      Dec 25 '16 at 12:12






    • 2





      the point is to be able to judge whether visiting said README is worthwhile, just from the answer.

      – muru
      Dec 25 '16 at 12:14











    • This is an adequate answer and it solves the problem exactly.

      – Ярослав Рахматуллин
      Oct 10 '18 at 23:16






    • 1





      This is an excellent and very comprehensive answer that works perfectly, thanks!. It's probably been updated since @muru had problems with it.

      – Brian Topping
      Oct 19 '18 at 20:21


















    25














    Try complete-alias, which solves this problem exactly.



    After install it you can use one generic function to complete many aliases like this:



    complete -F _complete_alias <myalias1>
    complete -F _complete_alias <myalias2>
    complete -F _complete_alias <myalias3>




    You may want to source the complete_alias file in every bash instance through .bash_profile or similar.



    installation



    mkdir ~/.bash_completion.d
    curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
    > ~/.bash_completion.d/complete_alias


    application



    source ~/.bash_completion.d/complete_alias

    alias container=docker container
    complete -F _complete_alias container


    container can now be autocompleted by the original _docker() completion handler;



    $ container l<Tab>
    logs ls

    $ container s<Tab>
    start stats stop





    share|improve this answer





















    • 2





      Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.

      – muru
      Dec 24 '16 at 7:55











    • @muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.

      – Cyker
      Dec 25 '16 at 12:12






    • 2





      the point is to be able to judge whether visiting said README is worthwhile, just from the answer.

      – muru
      Dec 25 '16 at 12:14











    • This is an adequate answer and it solves the problem exactly.

      – Ярослав Рахматуллин
      Oct 10 '18 at 23:16






    • 1





      This is an excellent and very comprehensive answer that works perfectly, thanks!. It's probably been updated since @muru had problems with it.

      – Brian Topping
      Oct 19 '18 at 20:21
















    25












    25








    25







    Try complete-alias, which solves this problem exactly.



    After install it you can use one generic function to complete many aliases like this:



    complete -F _complete_alias <myalias1>
    complete -F _complete_alias <myalias2>
    complete -F _complete_alias <myalias3>




    You may want to source the complete_alias file in every bash instance through .bash_profile or similar.



    installation



    mkdir ~/.bash_completion.d
    curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
    > ~/.bash_completion.d/complete_alias


    application



    source ~/.bash_completion.d/complete_alias

    alias container=docker container
    complete -F _complete_alias container


    container can now be autocompleted by the original _docker() completion handler;



    $ container l<Tab>
    logs ls

    $ container s<Tab>
    start stats stop





    share|improve this answer















    Try complete-alias, which solves this problem exactly.



    After install it you can use one generic function to complete many aliases like this:



    complete -F _complete_alias <myalias1>
    complete -F _complete_alias <myalias2>
    complete -F _complete_alias <myalias3>




    You may want to source the complete_alias file in every bash instance through .bash_profile or similar.



    installation



    mkdir ~/.bash_completion.d
    curl https://raw.githubusercontent.com/cykerway/complete-alias/master/completions/bash_completion.sh
    > ~/.bash_completion.d/complete_alias


    application



    source ~/.bash_completion.d/complete_alias

    alias container=docker container
    complete -F _complete_alias container


    container can now be autocompleted by the original _docker() completion handler;



    $ container l<Tab>
    logs ls

    $ container s<Tab>
    start stats stop






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Oct 11 '18 at 2:14









    Ярослав Рахматуллин

    5123 silver badges12 bronze badges




    5123 silver badges12 bronze badges










    answered Dec 24 '16 at 7:17









    CykerCyker

    1,5822 gold badges16 silver badges33 bronze badges




    1,5822 gold badges16 silver badges33 bronze badges








    • 2





      Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.

      – muru
      Dec 24 '16 at 7:55











    • @muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.

      – Cyker
      Dec 25 '16 at 12:12






    • 2





      the point is to be able to judge whether visiting said README is worthwhile, just from the answer.

      – muru
      Dec 25 '16 at 12:14











    • This is an adequate answer and it solves the problem exactly.

      – Ярослав Рахматуллин
      Oct 10 '18 at 23:16






    • 1





      This is an excellent and very comprehensive answer that works perfectly, thanks!. It's probably been updated since @muru had problems with it.

      – Brian Topping
      Oct 19 '18 at 20:21
















    • 2





      Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.

      – muru
      Dec 24 '16 at 7:55











    • @muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.

      – Cyker
      Dec 25 '16 at 12:12






    • 2





      the point is to be able to judge whether visiting said README is worthwhile, just from the answer.

      – muru
      Dec 25 '16 at 12:14











    • This is an adequate answer and it solves the problem exactly.

      – Ярослав Рахматуллин
      Oct 10 '18 at 23:16






    • 1





      This is an excellent and very comprehensive answer that works perfectly, thanks!. It's probably been updated since @muru had problems with it.

      – Brian Topping
      Oct 19 '18 at 20:21










    2




    2





    Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.

    – muru
    Dec 24 '16 at 7:55





    Given that this software seems to be yours, you could do better than this. Add instructions on installing it. Describe what it does and how to use it. Otherwise this is just spam.

    – muru
    Dec 24 '16 at 7:55













    @muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.

    – Cyker
    Dec 25 '16 at 12:12





    @muru I was thinking of pasting some code here but it's probably longer than accepted here. I'd assume people have no problem reading the Install section in a README file, and in this case it's only several lines.

    – Cyker
    Dec 25 '16 at 12:12




    2




    2





    the point is to be able to judge whether visiting said README is worthwhile, just from the answer.

    – muru
    Dec 25 '16 at 12:14





    the point is to be able to judge whether visiting said README is worthwhile, just from the answer.

    – muru
    Dec 25 '16 at 12:14













    This is an adequate answer and it solves the problem exactly.

    – Ярослав Рахматуллин
    Oct 10 '18 at 23:16





    This is an adequate answer and it solves the problem exactly.

    – Ярослав Рахматуллин
    Oct 10 '18 at 23:16




    1




    1





    This is an excellent and very comprehensive answer that works perfectly, thanks!. It's probably been updated since @muru had problems with it.

    – Brian Topping
    Oct 19 '18 at 20:21







    This is an excellent and very comprehensive answer that works perfectly, thanks!. It's probably been updated since @muru had problems with it.

    – Brian Topping
    Oct 19 '18 at 20:21













    3














    2018 answer



    You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.



    For package autocompletion:



    complete -c name_of_your_alias


    For command autocompletion:



    complete -F name_of_your_alias


    To check if your alias was added correctly:



    complete | grep name_of_your_alias


    Finally, to remove an alias from 'complete':



    complete -r name_of_your_alias


    In your case:



    complete -c apt-inst





    share|improve this answer





















    • 1





      Please don’t continue your answer in comments. If you have things to say, edit them into the answer.

      – G-Man
      May 21 '18 at 3:02






    • 2





      complete -F xxx does not work on OSX (It just shows the usage complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]) . Do you have any pointer? Thanks.

      – Anthony Kong
      Feb 28 at 22:20











    • Use this instead: unix.stackexchange.com/questions/224227/…

      – Rich
      Jun 5 at 8:20
















    3














    2018 answer



    You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.



    For package autocompletion:



    complete -c name_of_your_alias


    For command autocompletion:



    complete -F name_of_your_alias


    To check if your alias was added correctly:



    complete | grep name_of_your_alias


    Finally, to remove an alias from 'complete':



    complete -r name_of_your_alias


    In your case:



    complete -c apt-inst





    share|improve this answer





















    • 1





      Please don’t continue your answer in comments. If you have things to say, edit them into the answer.

      – G-Man
      May 21 '18 at 3:02






    • 2





      complete -F xxx does not work on OSX (It just shows the usage complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]) . Do you have any pointer? Thanks.

      – Anthony Kong
      Feb 28 at 22:20











    • Use this instead: unix.stackexchange.com/questions/224227/…

      – Rich
      Jun 5 at 8:20














    3












    3








    3







    2018 answer



    You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.



    For package autocompletion:



    complete -c name_of_your_alias


    For command autocompletion:



    complete -F name_of_your_alias


    To check if your alias was added correctly:



    complete | grep name_of_your_alias


    Finally, to remove an alias from 'complete':



    complete -r name_of_your_alias


    In your case:



    complete -c apt-inst





    share|improve this answer















    2018 answer



    You must add your alias to the program 'complete'. Depending the kind of autocompletion you want to achieve, you must use -c or -F.



    For package autocompletion:



    complete -c name_of_your_alias


    For command autocompletion:



    complete -F name_of_your_alias


    To check if your alias was added correctly:



    complete | grep name_of_your_alias


    Finally, to remove an alias from 'complete':



    complete -r name_of_your_alias


    In your case:



    complete -c apt-inst






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited May 21 '18 at 2:49

























    answered May 21 '18 at 2:42









    Adrian LopezAdrian Lopez

    1365 bronze badges




    1365 bronze badges








    • 1





      Please don’t continue your answer in comments. If you have things to say, edit them into the answer.

      – G-Man
      May 21 '18 at 3:02






    • 2





      complete -F xxx does not work on OSX (It just shows the usage complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]) . Do you have any pointer? Thanks.

      – Anthony Kong
      Feb 28 at 22:20











    • Use this instead: unix.stackexchange.com/questions/224227/…

      – Rich
      Jun 5 at 8:20














    • 1





      Please don’t continue your answer in comments. If you have things to say, edit them into the answer.

      – G-Man
      May 21 '18 at 3:02






    • 2





      complete -F xxx does not work on OSX (It just shows the usage complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]) . Do you have any pointer? Thanks.

      – Anthony Kong
      Feb 28 at 22:20











    • Use this instead: unix.stackexchange.com/questions/224227/…

      – Rich
      Jun 5 at 8:20








    1




    1





    Please don’t continue your answer in comments. If you have things to say, edit them into the answer.

    – G-Man
    May 21 '18 at 3:02





    Please don’t continue your answer in comments. If you have things to say, edit them into the answer.

    – G-Man
    May 21 '18 at 3:02




    2




    2





    complete -F xxx does not work on OSX (It just shows the usage complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]) . Do you have any pointer? Thanks.

    – Anthony Kong
    Feb 28 at 22:20





    complete -F xxx does not work on OSX (It just shows the usage complete: usage: complete [-abcdefgjksuv] [-pr] [-o option] [-A action] [-G globpat] [-W wordlist] [-P prefix] [-S suffix] [-X filterpat] [-F function] [-C command] [name ...]) . Do you have any pointer? Thanks.

    – Anthony Kong
    Feb 28 at 22:20













    Use this instead: unix.stackexchange.com/questions/224227/…

    – Rich
    Jun 5 at 8:20





    Use this instead: unix.stackexchange.com/questions/224227/…

    – Rich
    Jun 5 at 8:20











    2














    Here's the code from Shawn J. Goff's answer with some improvements:




    • Fixed syntax errors highlighted by shell-check, eg the first " of "$@" actually ended the function definition string.

    • Removed the return 0 so that the return value of the underlying function can be passed back to the caller.


    .



    # Wraps a completion function, eg for use with an alias.
    # Usage:
    # make-completion-wrapper <actual completion function> <name of new func.>
    # <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
    function make-completion-wrapper {
    local function_name="$2"
    local arg_count=$(( $#-3 ))
    local comp_function_name="$1"
    shift 2
    local function="function $function_name {
    (( COMP_CWORD += $arg_count ))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    }"
    eval "$function"
    # echo "$function"
    }
    export -f make-completion-wrapper





    share|improve this answer


























    • Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.

      – joelittlejohn
      Dec 19 '16 at 14:38






    • 1





      "Ending" the function definition string is on purpose; the point is to include arguments from the outer function into the COMP_WORDS array. If we use make-completion-wrapper _apt_get _apt_get_install apt-get install as an example, after shift 2, $@ contains apt-get install. The function definition includes apt-get install (the two literal words) when assigning COMP_WORDS.

      – Jeffery To
      Jun 3 at 19:58
















    2














    Here's the code from Shawn J. Goff's answer with some improvements:




    • Fixed syntax errors highlighted by shell-check, eg the first " of "$@" actually ended the function definition string.

    • Removed the return 0 so that the return value of the underlying function can be passed back to the caller.


    .



    # Wraps a completion function, eg for use with an alias.
    # Usage:
    # make-completion-wrapper <actual completion function> <name of new func.>
    # <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
    function make-completion-wrapper {
    local function_name="$2"
    local arg_count=$(( $#-3 ))
    local comp_function_name="$1"
    shift 2
    local function="function $function_name {
    (( COMP_CWORD += $arg_count ))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    }"
    eval "$function"
    # echo "$function"
    }
    export -f make-completion-wrapper





    share|improve this answer


























    • Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.

      – joelittlejohn
      Dec 19 '16 at 14:38






    • 1





      "Ending" the function definition string is on purpose; the point is to include arguments from the outer function into the COMP_WORDS array. If we use make-completion-wrapper _apt_get _apt_get_install apt-get install as an example, after shift 2, $@ contains apt-get install. The function definition includes apt-get install (the two literal words) when assigning COMP_WORDS.

      – Jeffery To
      Jun 3 at 19:58














    2












    2








    2







    Here's the code from Shawn J. Goff's answer with some improvements:




    • Fixed syntax errors highlighted by shell-check, eg the first " of "$@" actually ended the function definition string.

    • Removed the return 0 so that the return value of the underlying function can be passed back to the caller.


    .



    # Wraps a completion function, eg for use with an alias.
    # Usage:
    # make-completion-wrapper <actual completion function> <name of new func.>
    # <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
    function make-completion-wrapper {
    local function_name="$2"
    local arg_count=$(( $#-3 ))
    local comp_function_name="$1"
    shift 2
    local function="function $function_name {
    (( COMP_CWORD += $arg_count ))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    }"
    eval "$function"
    # echo "$function"
    }
    export -f make-completion-wrapper





    share|improve this answer















    Here's the code from Shawn J. Goff's answer with some improvements:




    • Fixed syntax errors highlighted by shell-check, eg the first " of "$@" actually ended the function definition string.

    • Removed the return 0 so that the return value of the underlying function can be passed back to the caller.


    .



    # Wraps a completion function, eg for use with an alias.
    # Usage:
    # make-completion-wrapper <actual completion function> <name of new func.>
    # <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
    function make-completion-wrapper {
    local function_name="$2"
    local arg_count=$(( $#-3 ))
    local comp_function_name="$1"
    shift 2
    local function="function $function_name {
    (( COMP_CWORD += $arg_count ))
    COMP_WORDS=( "$@" ${COMP_WORDS[@]:1} )
    "$comp_function_name"
    }"
    eval "$function"
    # echo "$function"
    }
    export -f make-completion-wrapper






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Apr 13 '17 at 12:37









    Community

    1




    1










    answered Sep 15 '16 at 13:05









    Tom HaleTom Hale

    8,3763 gold badges53 silver badges116 bronze badges




    8,3763 gold badges53 silver badges116 bronze badges













    • Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.

      – joelittlejohn
      Dec 19 '16 at 14:38






    • 1





      "Ending" the function definition string is on purpose; the point is to include arguments from the outer function into the COMP_WORDS array. If we use make-completion-wrapper _apt_get _apt_get_install apt-get install as an example, after shift 2, $@ contains apt-get install. The function definition includes apt-get install (the two literal words) when assigning COMP_WORDS.

      – Jeffery To
      Jun 3 at 19:58



















    • Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.

      – joelittlejohn
      Dec 19 '16 at 14:38






    • 1





      "Ending" the function definition string is on purpose; the point is to include arguments from the outer function into the COMP_WORDS array. If we use make-completion-wrapper _apt_get _apt_get_install apt-get install as an example, after shift 2, $@ contains apt-get install. The function definition includes apt-get install (the two literal words) when assigning COMP_WORDS.

      – Jeffery To
      Jun 3 at 19:58

















    Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.

    – joelittlejohn
    Dec 19 '16 at 14:38





    Doesn't work for me in Bash 4.3.46 I'm afraid. Shaun J. Goff's answer does work.

    – joelittlejohn
    Dec 19 '16 at 14:38




    1




    1





    "Ending" the function definition string is on purpose; the point is to include arguments from the outer function into the COMP_WORDS array. If we use make-completion-wrapper _apt_get _apt_get_install apt-get install as an example, after shift 2, $@ contains apt-get install. The function definition includes apt-get install (the two literal words) when assigning COMP_WORDS.

    – Jeffery To
    Jun 3 at 19:58





    "Ending" the function definition string is on purpose; the point is to include arguments from the outer function into the COMP_WORDS array. If we use make-completion-wrapper _apt_get _apt_get_install apt-get install as an example, after shift 2, $@ contains apt-get install. The function definition includes apt-get install (the two literal words) when assigning COMP_WORDS.

    – Jeffery To
    Jun 3 at 19:58











    0














    As an extension to @Giles answer, the following convenience function auto-names the wrapper generated by make-completion-wrapper to make it possible to define completion in one line:



    function complete-alias  {
    # uses make-completion-wrapper: https://unix.stackexchange.com/a/4220/50978
    # example usage
    # complete-alias _pass pshow pass show
    # complete-alias _pass pgen pass generate

    EXISTING_COMPLETION_FN=${1} && shift
    ALIAS=${1} && shift
    AUTOGEN_COMPLETION_FN="__autogen_completion_${ALIAS}"
    make-completion-wrapper ${EXISTING_COMPLETION_FN} ${AUTOGEN_COMPLETION_FN} ${*}
    complete -F ${AUTOGEN_COMPLETION_FN} ${ALIAS}
    }





    share|improve this answer




























      0














      As an extension to @Giles answer, the following convenience function auto-names the wrapper generated by make-completion-wrapper to make it possible to define completion in one line:



      function complete-alias  {
      # uses make-completion-wrapper: https://unix.stackexchange.com/a/4220/50978
      # example usage
      # complete-alias _pass pshow pass show
      # complete-alias _pass pgen pass generate

      EXISTING_COMPLETION_FN=${1} && shift
      ALIAS=${1} && shift
      AUTOGEN_COMPLETION_FN="__autogen_completion_${ALIAS}"
      make-completion-wrapper ${EXISTING_COMPLETION_FN} ${AUTOGEN_COMPLETION_FN} ${*}
      complete -F ${AUTOGEN_COMPLETION_FN} ${ALIAS}
      }





      share|improve this answer


























        0












        0








        0







        As an extension to @Giles answer, the following convenience function auto-names the wrapper generated by make-completion-wrapper to make it possible to define completion in one line:



        function complete-alias  {
        # uses make-completion-wrapper: https://unix.stackexchange.com/a/4220/50978
        # example usage
        # complete-alias _pass pshow pass show
        # complete-alias _pass pgen pass generate

        EXISTING_COMPLETION_FN=${1} && shift
        ALIAS=${1} && shift
        AUTOGEN_COMPLETION_FN="__autogen_completion_${ALIAS}"
        make-completion-wrapper ${EXISTING_COMPLETION_FN} ${AUTOGEN_COMPLETION_FN} ${*}
        complete -F ${AUTOGEN_COMPLETION_FN} ${ALIAS}
        }





        share|improve this answer













        As an extension to @Giles answer, the following convenience function auto-names the wrapper generated by make-completion-wrapper to make it possible to define completion in one line:



        function complete-alias  {
        # uses make-completion-wrapper: https://unix.stackexchange.com/a/4220/50978
        # example usage
        # complete-alias _pass pshow pass show
        # complete-alias _pass pgen pass generate

        EXISTING_COMPLETION_FN=${1} && shift
        ALIAS=${1} && shift
        AUTOGEN_COMPLETION_FN="__autogen_completion_${ALIAS}"
        make-completion-wrapper ${EXISTING_COMPLETION_FN} ${AUTOGEN_COMPLETION_FN} ${*}
        complete -F ${AUTOGEN_COMPLETION_FN} ${ALIAS}
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered 10 mins ago









        user84207user84207

        2952 gold badges6 silver badges18 bronze badges




        2952 gold badges6 silver badges18 bronze badges






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Unix & Linux Stack Exchange!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f4219%2fhow-do-i-get-bash-completion-for-command-aliases%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

            Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

            Nicolae Petrescu-Găină Cuprins Biografie | Opera | In memoriam | Varia | Controverse, incertitudini...