Alternative of bash's `history -p` in zsh?

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











up vote
2
down vote

favorite












In bash, history -p does history expansion on its argument; What is the alternative in zsh?










share|improve this question

























    up vote
    2
    down vote

    favorite












    In bash, history -p does history expansion on its argument; What is the alternative in zsh?










    share|improve this question























      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      In bash, history -p does history expansion on its argument; What is the alternative in zsh?










      share|improve this question













      In bash, history -p does history expansion on its argument; What is the alternative in zsh?







      zsh command-history history-expansion fc






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Sep 3 at 11:38









      HappyFace

      1018




      1018




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          I'm not aware of any, but see the histverify option which changes the behaviour of history expansion so that when you press enter on a line that contains history operators, the expansion is performed in the editor buffer without running the command.




          $ setopt histverify
          $ echo !!Enter
          $ echo setopt histverifyEnter
          setopt histverify


          Also note that in zsh like in csh (where history expansion comes from in the late 70s) history modifiers (like :h, :r, :A...) can also be applied to parameter expansions ($var:h...).



          And the whole history is accessible with the $history special associative array.



          Keys of that associative arrays are the history numbers, and contrary to regular associative arrays where the order is undefined, $history[@] is sorted in descending order on the numerical value of the key.



          So $history:0:1 is the last history entry. So, you can use parameter expansion operators to get the same result as with history expansion operators. For instance: !$:h, could be $$(z)history:0:1[-1]:h






          share|improve this answer





























            up vote
            0
            down vote













            In bash you can expand a command by doing this:



            history -p !23 


            So if in 23 you have ls -l the, return will be:



            ls 
            -l


            In zsh i am able to reproduce a similar (but not identical) behavior by doing this:



            history 23 23 | xargs -n1 | tail -n+2





            share|improve this answer


















            • 1




              ITYM history -p '!23'. In zsh, just use print -r -- $history[23]
              – Stéphane Chazelas
              Sep 3 at 15:02










            • I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(
              – Luciano Andress Martini
              Sep 3 at 15:04











            • Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'. I wrote that function using fc in the end.
              – HappyFace
              Sep 4 at 17:49










            • I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!
              – Luciano Andress Martini
              Sep 4 at 17:54











            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',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f466554%2falternative-of-bashs-history-p-in-zsh%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            I'm not aware of any, but see the histverify option which changes the behaviour of history expansion so that when you press enter on a line that contains history operators, the expansion is performed in the editor buffer without running the command.




            $ setopt histverify
            $ echo !!Enter
            $ echo setopt histverifyEnter
            setopt histverify


            Also note that in zsh like in csh (where history expansion comes from in the late 70s) history modifiers (like :h, :r, :A...) can also be applied to parameter expansions ($var:h...).



            And the whole history is accessible with the $history special associative array.



            Keys of that associative arrays are the history numbers, and contrary to regular associative arrays where the order is undefined, $history[@] is sorted in descending order on the numerical value of the key.



            So $history:0:1 is the last history entry. So, you can use parameter expansion operators to get the same result as with history expansion operators. For instance: !$:h, could be $$(z)history:0:1[-1]:h






            share|improve this answer


























              up vote
              2
              down vote













              I'm not aware of any, but see the histverify option which changes the behaviour of history expansion so that when you press enter on a line that contains history operators, the expansion is performed in the editor buffer without running the command.




              $ setopt histverify
              $ echo !!Enter
              $ echo setopt histverifyEnter
              setopt histverify


              Also note that in zsh like in csh (where history expansion comes from in the late 70s) history modifiers (like :h, :r, :A...) can also be applied to parameter expansions ($var:h...).



              And the whole history is accessible with the $history special associative array.



              Keys of that associative arrays are the history numbers, and contrary to regular associative arrays where the order is undefined, $history[@] is sorted in descending order on the numerical value of the key.



              So $history:0:1 is the last history entry. So, you can use parameter expansion operators to get the same result as with history expansion operators. For instance: !$:h, could be $$(z)history:0:1[-1]:h






              share|improve this answer
























                up vote
                2
                down vote










                up vote
                2
                down vote









                I'm not aware of any, but see the histverify option which changes the behaviour of history expansion so that when you press enter on a line that contains history operators, the expansion is performed in the editor buffer without running the command.




                $ setopt histverify
                $ echo !!Enter
                $ echo setopt histverifyEnter
                setopt histverify


                Also note that in zsh like in csh (where history expansion comes from in the late 70s) history modifiers (like :h, :r, :A...) can also be applied to parameter expansions ($var:h...).



                And the whole history is accessible with the $history special associative array.



                Keys of that associative arrays are the history numbers, and contrary to regular associative arrays where the order is undefined, $history[@] is sorted in descending order on the numerical value of the key.



                So $history:0:1 is the last history entry. So, you can use parameter expansion operators to get the same result as with history expansion operators. For instance: !$:h, could be $$(z)history:0:1[-1]:h






                share|improve this answer














                I'm not aware of any, but see the histverify option which changes the behaviour of history expansion so that when you press enter on a line that contains history operators, the expansion is performed in the editor buffer without running the command.




                $ setopt histverify
                $ echo !!Enter
                $ echo setopt histverifyEnter
                setopt histverify


                Also note that in zsh like in csh (where history expansion comes from in the late 70s) history modifiers (like :h, :r, :A...) can also be applied to parameter expansions ($var:h...).



                And the whole history is accessible with the $history special associative array.



                Keys of that associative arrays are the history numbers, and contrary to regular associative arrays where the order is undefined, $history[@] is sorted in descending order on the numerical value of the key.



                So $history:0:1 is the last history entry. So, you can use parameter expansion operators to get the same result as with history expansion operators. For instance: !$:h, could be $$(z)history:0:1[-1]:h







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Sep 3 at 16:19

























                answered Sep 3 at 14:21









                Stéphane Chazelas

                286k53527866




                286k53527866






















                    up vote
                    0
                    down vote













                    In bash you can expand a command by doing this:



                    history -p !23 


                    So if in 23 you have ls -l the, return will be:



                    ls 
                    -l


                    In zsh i am able to reproduce a similar (but not identical) behavior by doing this:



                    history 23 23 | xargs -n1 | tail -n+2





                    share|improve this answer


















                    • 1




                      ITYM history -p '!23'. In zsh, just use print -r -- $history[23]
                      – Stéphane Chazelas
                      Sep 3 at 15:02










                    • I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(
                      – Luciano Andress Martini
                      Sep 3 at 15:04











                    • Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'. I wrote that function using fc in the end.
                      – HappyFace
                      Sep 4 at 17:49










                    • I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!
                      – Luciano Andress Martini
                      Sep 4 at 17:54















                    up vote
                    0
                    down vote













                    In bash you can expand a command by doing this:



                    history -p !23 


                    So if in 23 you have ls -l the, return will be:



                    ls 
                    -l


                    In zsh i am able to reproduce a similar (but not identical) behavior by doing this:



                    history 23 23 | xargs -n1 | tail -n+2





                    share|improve this answer


















                    • 1




                      ITYM history -p '!23'. In zsh, just use print -r -- $history[23]
                      – Stéphane Chazelas
                      Sep 3 at 15:02










                    • I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(
                      – Luciano Andress Martini
                      Sep 3 at 15:04











                    • Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'. I wrote that function using fc in the end.
                      – HappyFace
                      Sep 4 at 17:49










                    • I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!
                      – Luciano Andress Martini
                      Sep 4 at 17:54













                    up vote
                    0
                    down vote










                    up vote
                    0
                    down vote









                    In bash you can expand a command by doing this:



                    history -p !23 


                    So if in 23 you have ls -l the, return will be:



                    ls 
                    -l


                    In zsh i am able to reproduce a similar (but not identical) behavior by doing this:



                    history 23 23 | xargs -n1 | tail -n+2





                    share|improve this answer














                    In bash you can expand a command by doing this:



                    history -p !23 


                    So if in 23 you have ls -l the, return will be:



                    ls 
                    -l


                    In zsh i am able to reproduce a similar (but not identical) behavior by doing this:



                    history 23 23 | xargs -n1 | tail -n+2






                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 7 at 10:47









                    Rui F Ribeiro

                    36.8k1272117




                    36.8k1272117










                    answered Sep 3 at 14:57









                    Luciano Andress Martini

                    3,153829




                    3,153829







                    • 1




                      ITYM history -p '!23'. In zsh, just use print -r -- $history[23]
                      – Stéphane Chazelas
                      Sep 3 at 15:02










                    • I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(
                      – Luciano Andress Martini
                      Sep 3 at 15:04











                    • Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'. I wrote that function using fc in the end.
                      – HappyFace
                      Sep 4 at 17:49










                    • I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!
                      – Luciano Andress Martini
                      Sep 4 at 17:54













                    • 1




                      ITYM history -p '!23'. In zsh, just use print -r -- $history[23]
                      – Stéphane Chazelas
                      Sep 3 at 15:02










                    • I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(
                      – Luciano Andress Martini
                      Sep 3 at 15:04











                    • Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'. I wrote that function using fc in the end.
                      – HappyFace
                      Sep 4 at 17:49










                    • I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!
                      – Luciano Andress Martini
                      Sep 4 at 17:54








                    1




                    1




                    ITYM history -p '!23'. In zsh, just use print -r -- $history[23]
                    – Stéphane Chazelas
                    Sep 3 at 15:02




                    ITYM history -p '!23'. In zsh, just use print -r -- $history[23]
                    – Stéphane Chazelas
                    Sep 3 at 15:02












                    I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(
                    – Luciano Andress Martini
                    Sep 3 at 15:04





                    I tried but nothing happens =(. Try history -p !23 instead of history -p '!23'... for what is really that ? =(
                    – Luciano Andress Martini
                    Sep 3 at 15:04













                    Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'. I wrote that function using fc in the end.
                    – HappyFace
                    Sep 4 at 17:49




                    Thanks, but I need more sophisticated behavior; I wanted to run history -p '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'. I wrote that function using fc in the end.
                    – HappyFace
                    Sep 4 at 17:49












                    I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!
                    – Luciano Andress Martini
                    Sep 4 at 17:54





                    I cant imagine the results you want to got. I just received "History Expansion Failed" (in bash). Can you post in you question what you have, and what exactly you want to get or do? Thank you. Maybe i have a easy solution!
                    – Luciano Andress Martini
                    Sep 4 at 17:54


















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466554%2falternative-of-bashs-history-p-in-zsh%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

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

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?