Treat command like another for completion purposes

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP












2















My zshrc includes the following function to create a directory and then enter it:



function mcd () 
mkdir -p "$*" && cd "$*"



The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/ and then press Tab, the message



_mtools_drives:3: command not found: mtoolstest


is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir would be: zsh should offer me the names of existing directories.



How do I tell zsh that for completion purposes, it should treat mcd the same as mkdir?










share|improve this question




























    2















    My zshrc includes the following function to create a directory and then enter it:



    function mcd () 
    mkdir -p "$*" && cd "$*"



    The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/ and then press Tab, the message



    _mtools_drives:3: command not found: mtoolstest


    is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir would be: zsh should offer me the names of existing directories.



    How do I tell zsh that for completion purposes, it should treat mcd the same as mkdir?










    share|improve this question


























      2












      2








      2








      My zshrc includes the following function to create a directory and then enter it:



      function mcd () 
      mkdir -p "$*" && cd "$*"



      The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/ and then press Tab, the message



      _mtools_drives:3: command not found: mtoolstest


      is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir would be: zsh should offer me the names of existing directories.



      How do I tell zsh that for completion purposes, it should treat mcd the same as mkdir?










      share|improve this question
















      My zshrc includes the following function to create a directory and then enter it:



      function mcd () 
      mkdir -p "$*" && cd "$*"



      The function itself works fine but I get odd behavior with completion. If I start typing e.g. mcd ~/ and then press Tab, the message



      _mtools_drives:3: command not found: mtoolstest


      is inserted at the insertion point and nothing is completed. What I want is for the command to be completed just as mkdir would be: zsh should offer me the names of existing directories.



      How do I tell zsh that for completion purposes, it should treat mcd the same as mkdir?







      zsh autocomplete






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 25 at 21:20









      Gilles

      536k12810821600




      536k12810821600










      asked Jan 24 at 5:00









      bdeshambdesham

      313212




      313212




















          2 Answers
          2






          active

          oldest

          votes


















          2














          The following snippet causes mcd to be completed like mkdir:



          compdefas () 
          if (($+_comps[$1])); then
          compdef $_comps[$1] $^@[2,-1]=$1
          fi

          compdefas mkdir mcd


          The way it works is to look up the current completion setting for mkdir. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps. Thus compdef $_comps[mkdir] mcd declares that mcd should be completed in the same way that mkdir is completed right now.



          The function above adds a few niceties:



          • The test for (($+_comps[$1])) ensures that if $1 doesn't have a specified completion method then no completion method is set for the other arguments.


          • $@[2,-1] is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually $^@[a,-1] so that the text around the array expansion is replicated for each array element.


          • =$1 sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function _gzip handles both gzip and gunzip as well as pigz and unpigz; compdef _gzip foo makes foo use the default behavior of _gzip while compdef _gzip foo=pigz makes foo use the behavior of _gzip when it completes for pigz.


          Turning to your specific case, the default completion for mkdir not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files).



          compdef _directories mcd


          The reason you were getting these bizarre-looking completions for mcd is that it's the name of a command from a once moderately widespread suite of commands mtools.






          share|improve this answer























          • If I issue command mcd without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory. mkdir will give mkdir: missing operand which look nicer than our mcd, how to have the same behavior as mkdir?

            – Tuyen Pham
            Jan 27 at 6:31






          • 1





            @TuyenPham See if my function inspires you.

            – Gilles
            Jan 27 at 18:54











          • Thanks, that's truly mkdir wrapper, but should we still need to have compdefas? As I test your mkcd function, it works well with completion.

            – Tuyen Pham
            Jan 28 at 2:03


















          1














          compdef uses the following form:



          compdef [ -ane ] function name


          so one idea would be to point the name of mcd at the _mkdir function via the following command:



          compdef _mkdir mcd


          however this is slightly incorrect as mkdir completes various flags that should probably not also be given to cd. More direct would be to complete on directories:



          compdef _directories mcd





          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%2f496379%2ftreat-command-like-another-for-completion-purposes%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            2














            The following snippet causes mcd to be completed like mkdir:



            compdefas () 
            if (($+_comps[$1])); then
            compdef $_comps[$1] $^@[2,-1]=$1
            fi

            compdefas mkdir mcd


            The way it works is to look up the current completion setting for mkdir. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps. Thus compdef $_comps[mkdir] mcd declares that mcd should be completed in the same way that mkdir is completed right now.



            The function above adds a few niceties:



            • The test for (($+_comps[$1])) ensures that if $1 doesn't have a specified completion method then no completion method is set for the other arguments.


            • $@[2,-1] is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually $^@[a,-1] so that the text around the array expansion is replicated for each array element.


            • =$1 sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function _gzip handles both gzip and gunzip as well as pigz and unpigz; compdef _gzip foo makes foo use the default behavior of _gzip while compdef _gzip foo=pigz makes foo use the behavior of _gzip when it completes for pigz.


            Turning to your specific case, the default completion for mkdir not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files).



            compdef _directories mcd


            The reason you were getting these bizarre-looking completions for mcd is that it's the name of a command from a once moderately widespread suite of commands mtools.






            share|improve this answer























            • If I issue command mcd without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory. mkdir will give mkdir: missing operand which look nicer than our mcd, how to have the same behavior as mkdir?

              – Tuyen Pham
              Jan 27 at 6:31






            • 1





              @TuyenPham See if my function inspires you.

              – Gilles
              Jan 27 at 18:54











            • Thanks, that's truly mkdir wrapper, but should we still need to have compdefas? As I test your mkcd function, it works well with completion.

              – Tuyen Pham
              Jan 28 at 2:03















            2














            The following snippet causes mcd to be completed like mkdir:



            compdefas () 
            if (($+_comps[$1])); then
            compdef $_comps[$1] $^@[2,-1]=$1
            fi

            compdefas mkdir mcd


            The way it works is to look up the current completion setting for mkdir. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps. Thus compdef $_comps[mkdir] mcd declares that mcd should be completed in the same way that mkdir is completed right now.



            The function above adds a few niceties:



            • The test for (($+_comps[$1])) ensures that if $1 doesn't have a specified completion method then no completion method is set for the other arguments.


            • $@[2,-1] is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually $^@[a,-1] so that the text around the array expansion is replicated for each array element.


            • =$1 sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function _gzip handles both gzip and gunzip as well as pigz and unpigz; compdef _gzip foo makes foo use the default behavior of _gzip while compdef _gzip foo=pigz makes foo use the behavior of _gzip when it completes for pigz.


            Turning to your specific case, the default completion for mkdir not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files).



            compdef _directories mcd


            The reason you were getting these bizarre-looking completions for mcd is that it's the name of a command from a once moderately widespread suite of commands mtools.






            share|improve this answer























            • If I issue command mcd without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory. mkdir will give mkdir: missing operand which look nicer than our mcd, how to have the same behavior as mkdir?

              – Tuyen Pham
              Jan 27 at 6:31






            • 1





              @TuyenPham See if my function inspires you.

              – Gilles
              Jan 27 at 18:54











            • Thanks, that's truly mkdir wrapper, but should we still need to have compdefas? As I test your mkcd function, it works well with completion.

              – Tuyen Pham
              Jan 28 at 2:03













            2












            2








            2







            The following snippet causes mcd to be completed like mkdir:



            compdefas () 
            if (($+_comps[$1])); then
            compdef $_comps[$1] $^@[2,-1]=$1
            fi

            compdefas mkdir mcd


            The way it works is to look up the current completion setting for mkdir. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps. Thus compdef $_comps[mkdir] mcd declares that mcd should be completed in the same way that mkdir is completed right now.



            The function above adds a few niceties:



            • The test for (($+_comps[$1])) ensures that if $1 doesn't have a specified completion method then no completion method is set for the other arguments.


            • $@[2,-1] is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually $^@[a,-1] so that the text around the array expansion is replicated for each array element.


            • =$1 sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function _gzip handles both gzip and gunzip as well as pigz and unpigz; compdef _gzip foo makes foo use the default behavior of _gzip while compdef _gzip foo=pigz makes foo use the behavior of _gzip when it completes for pigz.


            Turning to your specific case, the default completion for mkdir not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files).



            compdef _directories mcd


            The reason you were getting these bizarre-looking completions for mcd is that it's the name of a command from a once moderately widespread suite of commands mtools.






            share|improve this answer













            The following snippet causes mcd to be completed like mkdir:



            compdefas () 
            if (($+_comps[$1])); then
            compdef $_comps[$1] $^@[2,-1]=$1
            fi

            compdefas mkdir mcd


            The way it works is to look up the current completion setting for mkdir. The completion code for a function (generally the name of a completion function) is stored in the associative array _comps. Thus compdef $_comps[mkdir] mcd declares that mcd should be completed in the same way that mkdir is completed right now.



            The function above adds a few niceties:



            • The test for (($+_comps[$1])) ensures that if $1 doesn't have a specified completion method then no completion method is set for the other arguments.


            • $@[2,-1] is the list of arguments to the function starting with the second one, so you can specify more than one command name to define completions for. It's actually $^@[a,-1] so that the text around the array expansion is replicated for each array element.


            • =$1 sets the service name to use. This matters only for a few commands whose completion function handles several closely-related commands. For example the completion function _gzip handles both gzip and gunzip as well as pigz and unpigz; compdef _gzip foo makes foo use the default behavior of _gzip while compdef _gzip foo=pigz makes foo use the behavior of _gzip when it completes for pigz.


            Turning to your specific case, the default completion for mkdir not only offers directories, but also options, which your function does not support. So you'd actually be better off defining mcd as just completing existing directories. Zsh comes with a helper function for that (an undocumented wrapper around _files).



            compdef _directories mcd


            The reason you were getting these bizarre-looking completions for mcd is that it's the name of a command from a once moderately widespread suite of commands mtools.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 25 at 21:57









            GillesGilles

            536k12810821600




            536k12810821600












            • If I issue command mcd without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory. mkdir will give mkdir: missing operand which look nicer than our mcd, how to have the same behavior as mkdir?

              – Tuyen Pham
              Jan 27 at 6:31






            • 1





              @TuyenPham See if my function inspires you.

              – Gilles
              Jan 27 at 18:54











            • Thanks, that's truly mkdir wrapper, but should we still need to have compdefas? As I test your mkcd function, it works well with completion.

              – Tuyen Pham
              Jan 28 at 2:03

















            • If I issue command mcd without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory. mkdir will give mkdir: missing operand which look nicer than our mcd, how to have the same behavior as mkdir?

              – Tuyen Pham
              Jan 27 at 6:31






            • 1





              @TuyenPham See if my function inspires you.

              – Gilles
              Jan 27 at 18:54











            • Thanks, that's truly mkdir wrapper, but should we still need to have compdefas? As I test your mkcd function, it works well with completion.

              – Tuyen Pham
              Jan 28 at 2:03
















            If I issue command mcd without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory. mkdir will give mkdir: missing operand which look nicer than our mcd, how to have the same behavior as mkdir?

            – Tuyen Pham
            Jan 27 at 6:31





            If I issue command mcd without any argument, then it echos mkdir: cannot create directory ‘’: No such file or directory. mkdir will give mkdir: missing operand which look nicer than our mcd, how to have the same behavior as mkdir?

            – Tuyen Pham
            Jan 27 at 6:31




            1




            1





            @TuyenPham See if my function inspires you.

            – Gilles
            Jan 27 at 18:54





            @TuyenPham See if my function inspires you.

            – Gilles
            Jan 27 at 18:54













            Thanks, that's truly mkdir wrapper, but should we still need to have compdefas? As I test your mkcd function, it works well with completion.

            – Tuyen Pham
            Jan 28 at 2:03





            Thanks, that's truly mkdir wrapper, but should we still need to have compdefas? As I test your mkcd function, it works well with completion.

            – Tuyen Pham
            Jan 28 at 2:03













            1














            compdef uses the following form:



            compdef [ -ane ] function name


            so one idea would be to point the name of mcd at the _mkdir function via the following command:



            compdef _mkdir mcd


            however this is slightly incorrect as mkdir completes various flags that should probably not also be given to cd. More direct would be to complete on directories:



            compdef _directories mcd





            share|improve this answer



























              1














              compdef uses the following form:



              compdef [ -ane ] function name


              so one idea would be to point the name of mcd at the _mkdir function via the following command:



              compdef _mkdir mcd


              however this is slightly incorrect as mkdir completes various flags that should probably not also be given to cd. More direct would be to complete on directories:



              compdef _directories mcd





              share|improve this answer

























                1












                1








                1







                compdef uses the following form:



                compdef [ -ane ] function name


                so one idea would be to point the name of mcd at the _mkdir function via the following command:



                compdef _mkdir mcd


                however this is slightly incorrect as mkdir completes various flags that should probably not also be given to cd. More direct would be to complete on directories:



                compdef _directories mcd





                share|improve this answer













                compdef uses the following form:



                compdef [ -ane ] function name


                so one idea would be to point the name of mcd at the _mkdir function via the following command:



                compdef _mkdir mcd


                however this is slightly incorrect as mkdir completes various flags that should probably not also be given to cd. More direct would be to complete on directories:



                compdef _directories mcd






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jan 24 at 5:26









                thrigthrig

                24.9k23157




                24.9k23157



























                    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%2f496379%2ftreat-command-like-another-for-completion-purposes%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

                    How to check contact read email or not when send email to Individual?

                    Bahrain

                    Postfix configuration issue with fips on centos 7; mailgun relay