How can I alias a history expansion in zsh?

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











up vote
3
down vote

favorite












I want this to work (it needs extendedglob and histsubstpattern):



alias ri='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'


But it doesn't:



$ alias sss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' 
$ echo /Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
/Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
$ sss
zsh: command not found: Pocket


I wouldn't mind using a function instead of an alias, but the result was the same.



I even tried export ss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' and then doing $ss, but that failed with zsh: command not found: ^(#b)E(?)^E$(l:2::0:)$((match[1]+1)).



Using eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' also fails with zsh: command not found: Pocket.



Update: Related (possibly duplicate) questions found:



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



https://stackoverflow.com/questions/27494753/how-to-get-last-command-run-without-using



https://stackoverflow.com/questions/48696876/using-history-expansion-in-a-bash-alias-or-function










share|improve this question



























    up vote
    3
    down vote

    favorite












    I want this to work (it needs extendedglob and histsubstpattern):



    alias ri='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'


    But it doesn't:



    $ alias sss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' 
    $ echo /Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
    /Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
    $ sss
    zsh: command not found: Pocket


    I wouldn't mind using a function instead of an alias, but the result was the same.



    I even tried export ss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' and then doing $ss, but that failed with zsh: command not found: ^(#b)E(?)^E$(l:2::0:)$((match[1]+1)).



    Using eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' also fails with zsh: command not found: Pocket.



    Update: Related (possibly duplicate) questions found:



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



    https://stackoverflow.com/questions/27494753/how-to-get-last-command-run-without-using



    https://stackoverflow.com/questions/48696876/using-history-expansion-in-a-bash-alias-or-function










    share|improve this question

























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      I want this to work (it needs extendedglob and histsubstpattern):



      alias ri='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'


      But it doesn't:



      $ alias sss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' 
      $ echo /Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
      /Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
      $ sss
      zsh: command not found: Pocket


      I wouldn't mind using a function instead of an alias, but the result was the same.



      I even tried export ss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' and then doing $ss, but that failed with zsh: command not found: ^(#b)E(?)^E$(l:2::0:)$((match[1]+1)).



      Using eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' also fails with zsh: command not found: Pocket.



      Update: Related (possibly duplicate) questions found:



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



      https://stackoverflow.com/questions/27494753/how-to-get-last-command-run-without-using



      https://stackoverflow.com/questions/48696876/using-history-expansion-in-a-bash-alias-or-function










      share|improve this question















      I want this to work (it needs extendedglob and histsubstpattern):



      alias ri='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))'


      But it doesn't:



      $ alias sss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' 
      $ echo /Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
      /Users/evar/Downloads/Video/Teenage_Mutant_Ninja_Turtles_2003_S02E01_DVDRip_30NAMA.mkv
      $ sss
      zsh: command not found: Pocket


      I wouldn't mind using a function instead of an alias, but the result was the same.



      I even tried export ss='^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' and then doing $ss, but that failed with zsh: command not found: ^(#b)E(?)^E$(l:2::0:)$((match[1]+1)).



      Using eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' also fails with zsh: command not found: Pocket.



      Update: Related (possibly duplicate) questions found:



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



      https://stackoverflow.com/questions/27494753/how-to-get-last-command-run-without-using



      https://stackoverflow.com/questions/48696876/using-history-expansion-in-a-bash-alias-or-function







      zsh alias command-history history-expansion






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Sep 3 at 14:13

























      asked Sep 3 at 10:24









      HappyFace

      1018




      1018




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          You can't, history expansion happens before alias or parameter expansion.



          I personally hate history expansion and is the first thing I disable.



          Here, instead of aliasing a history expansion, I'd suggest creating a widget that increments a E<n> number left of the cursor:



          increment-episode() 
          emulate -L zsh
          setopt extendedglob
          LBUFFER=$LBUFFER/(#b)(*E)(<->)/$match[1]$(l:$#match[2]::0:)$((match[2]+1))


          zle -N increment-episode

          bindkey 'e+' increment-episode


          And then, you just press Up and then Alt++ and you have a visual feedback of what's going on at every stage and can undo/redo/adapt at will, and not work blindly like with csh history expansion (a feature from the 70s that IMO made sense then but not so much now that we have faster and more capable terminals and line-editors).



          But if you really wanted to blindly evaluate the code in the previous command in the history with the number after E incremented, you could do:



          rerun-with-next-episode() 
          emulate -L zsh
          setopt extendedglob
          local new
          new=$$history:0:1/(#b)E(<->)/E$(l:$#match[1]::0:)$((match[1]+1))

          # display it
          print -ru2 -- $new

          # put it on the history
          print -rs -- $new

          # evaluate it
          eval -- $new






          share|improve this answer






















          • Is the setopt extendedglob locally scoped? If not, can it not be locally scoped? And why does the history expansion bother you enough to warrant disabling? Any specific cons?
            – HappyFace
            Sep 3 at 11:08






          • 1




            @HappyFace, my main concern is unwanted expansions of !s. emulate -L resets the options to a sane default and makes options (including ones set later on) locally scoped. If you want extendedglob set globally (which I'd recommend for interactive use), just add setopt extendedglob to your ~/.zshrc.
            – Stéphane Chazelas
            Sep 3 at 11:10











          • BTW, I don't understand why eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' is failing. Shouldn't evaling a command be just like typing it? and thank you, things finally worked out. :D
            – HappyFace
            Sep 3 at 13:43






          • 1




            @HappyFace, history expansion is only done upon input lines coming from zle. Think of the history number for instance. It wouldn't make sense in eval
            – Stéphane Chazelas
            Sep 3 at 13:49







          • 1




            @HappyFace, see also the edit for possible improvements to your approach.
            – Stéphane Chazelas
            Sep 3 at 14:00











          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%2f466540%2fhow-can-i-alias-a-history-expansion-in-zsh%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          2
          down vote



          accepted










          You can't, history expansion happens before alias or parameter expansion.



          I personally hate history expansion and is the first thing I disable.



          Here, instead of aliasing a history expansion, I'd suggest creating a widget that increments a E<n> number left of the cursor:



          increment-episode() 
          emulate -L zsh
          setopt extendedglob
          LBUFFER=$LBUFFER/(#b)(*E)(<->)/$match[1]$(l:$#match[2]::0:)$((match[2]+1))


          zle -N increment-episode

          bindkey 'e+' increment-episode


          And then, you just press Up and then Alt++ and you have a visual feedback of what's going on at every stage and can undo/redo/adapt at will, and not work blindly like with csh history expansion (a feature from the 70s that IMO made sense then but not so much now that we have faster and more capable terminals and line-editors).



          But if you really wanted to blindly evaluate the code in the previous command in the history with the number after E incremented, you could do:



          rerun-with-next-episode() 
          emulate -L zsh
          setopt extendedglob
          local new
          new=$$history:0:1/(#b)E(<->)/E$(l:$#match[1]::0:)$((match[1]+1))

          # display it
          print -ru2 -- $new

          # put it on the history
          print -rs -- $new

          # evaluate it
          eval -- $new






          share|improve this answer






















          • Is the setopt extendedglob locally scoped? If not, can it not be locally scoped? And why does the history expansion bother you enough to warrant disabling? Any specific cons?
            – HappyFace
            Sep 3 at 11:08






          • 1




            @HappyFace, my main concern is unwanted expansions of !s. emulate -L resets the options to a sane default and makes options (including ones set later on) locally scoped. If you want extendedglob set globally (which I'd recommend for interactive use), just add setopt extendedglob to your ~/.zshrc.
            – Stéphane Chazelas
            Sep 3 at 11:10











          • BTW, I don't understand why eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' is failing. Shouldn't evaling a command be just like typing it? and thank you, things finally worked out. :D
            – HappyFace
            Sep 3 at 13:43






          • 1




            @HappyFace, history expansion is only done upon input lines coming from zle. Think of the history number for instance. It wouldn't make sense in eval
            – Stéphane Chazelas
            Sep 3 at 13:49







          • 1




            @HappyFace, see also the edit for possible improvements to your approach.
            – Stéphane Chazelas
            Sep 3 at 14:00















          up vote
          2
          down vote



          accepted










          You can't, history expansion happens before alias or parameter expansion.



          I personally hate history expansion and is the first thing I disable.



          Here, instead of aliasing a history expansion, I'd suggest creating a widget that increments a E<n> number left of the cursor:



          increment-episode() 
          emulate -L zsh
          setopt extendedglob
          LBUFFER=$LBUFFER/(#b)(*E)(<->)/$match[1]$(l:$#match[2]::0:)$((match[2]+1))


          zle -N increment-episode

          bindkey 'e+' increment-episode


          And then, you just press Up and then Alt++ and you have a visual feedback of what's going on at every stage and can undo/redo/adapt at will, and not work blindly like with csh history expansion (a feature from the 70s that IMO made sense then but not so much now that we have faster and more capable terminals and line-editors).



          But if you really wanted to blindly evaluate the code in the previous command in the history with the number after E incremented, you could do:



          rerun-with-next-episode() 
          emulate -L zsh
          setopt extendedglob
          local new
          new=$$history:0:1/(#b)E(<->)/E$(l:$#match[1]::0:)$((match[1]+1))

          # display it
          print -ru2 -- $new

          # put it on the history
          print -rs -- $new

          # evaluate it
          eval -- $new






          share|improve this answer






















          • Is the setopt extendedglob locally scoped? If not, can it not be locally scoped? And why does the history expansion bother you enough to warrant disabling? Any specific cons?
            – HappyFace
            Sep 3 at 11:08






          • 1




            @HappyFace, my main concern is unwanted expansions of !s. emulate -L resets the options to a sane default and makes options (including ones set later on) locally scoped. If you want extendedglob set globally (which I'd recommend for interactive use), just add setopt extendedglob to your ~/.zshrc.
            – Stéphane Chazelas
            Sep 3 at 11:10











          • BTW, I don't understand why eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' is failing. Shouldn't evaling a command be just like typing it? and thank you, things finally worked out. :D
            – HappyFace
            Sep 3 at 13:43






          • 1




            @HappyFace, history expansion is only done upon input lines coming from zle. Think of the history number for instance. It wouldn't make sense in eval
            – Stéphane Chazelas
            Sep 3 at 13:49







          • 1




            @HappyFace, see also the edit for possible improvements to your approach.
            – Stéphane Chazelas
            Sep 3 at 14:00













          up vote
          2
          down vote



          accepted







          up vote
          2
          down vote



          accepted






          You can't, history expansion happens before alias or parameter expansion.



          I personally hate history expansion and is the first thing I disable.



          Here, instead of aliasing a history expansion, I'd suggest creating a widget that increments a E<n> number left of the cursor:



          increment-episode() 
          emulate -L zsh
          setopt extendedglob
          LBUFFER=$LBUFFER/(#b)(*E)(<->)/$match[1]$(l:$#match[2]::0:)$((match[2]+1))


          zle -N increment-episode

          bindkey 'e+' increment-episode


          And then, you just press Up and then Alt++ and you have a visual feedback of what's going on at every stage and can undo/redo/adapt at will, and not work blindly like with csh history expansion (a feature from the 70s that IMO made sense then but not so much now that we have faster and more capable terminals and line-editors).



          But if you really wanted to blindly evaluate the code in the previous command in the history with the number after E incremented, you could do:



          rerun-with-next-episode() 
          emulate -L zsh
          setopt extendedglob
          local new
          new=$$history:0:1/(#b)E(<->)/E$(l:$#match[1]::0:)$((match[1]+1))

          # display it
          print -ru2 -- $new

          # put it on the history
          print -rs -- $new

          # evaluate it
          eval -- $new






          share|improve this answer














          You can't, history expansion happens before alias or parameter expansion.



          I personally hate history expansion and is the first thing I disable.



          Here, instead of aliasing a history expansion, I'd suggest creating a widget that increments a E<n> number left of the cursor:



          increment-episode() 
          emulate -L zsh
          setopt extendedglob
          LBUFFER=$LBUFFER/(#b)(*E)(<->)/$match[1]$(l:$#match[2]::0:)$((match[2]+1))


          zle -N increment-episode

          bindkey 'e+' increment-episode


          And then, you just press Up and then Alt++ and you have a visual feedback of what's going on at every stage and can undo/redo/adapt at will, and not work blindly like with csh history expansion (a feature from the 70s that IMO made sense then but not so much now that we have faster and more capable terminals and line-editors).



          But if you really wanted to blindly evaluate the code in the previous command in the history with the number after E incremented, you could do:



          rerun-with-next-episode() 
          emulate -L zsh
          setopt extendedglob
          local new
          new=$$history:0:1/(#b)E(<->)/E$(l:$#match[1]::0:)$((match[1]+1))

          # display it
          print -ru2 -- $new

          # put it on the history
          print -rs -- $new

          # evaluate it
          eval -- $new







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 3 at 14:10

























          answered Sep 3 at 11:04









          Stéphane Chazelas

          286k53527866




          286k53527866











          • Is the setopt extendedglob locally scoped? If not, can it not be locally scoped? And why does the history expansion bother you enough to warrant disabling? Any specific cons?
            – HappyFace
            Sep 3 at 11:08






          • 1




            @HappyFace, my main concern is unwanted expansions of !s. emulate -L resets the options to a sane default and makes options (including ones set later on) locally scoped. If you want extendedglob set globally (which I'd recommend for interactive use), just add setopt extendedglob to your ~/.zshrc.
            – Stéphane Chazelas
            Sep 3 at 11:10











          • BTW, I don't understand why eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' is failing. Shouldn't evaling a command be just like typing it? and thank you, things finally worked out. :D
            – HappyFace
            Sep 3 at 13:43






          • 1




            @HappyFace, history expansion is only done upon input lines coming from zle. Think of the history number for instance. It wouldn't make sense in eval
            – Stéphane Chazelas
            Sep 3 at 13:49







          • 1




            @HappyFace, see also the edit for possible improvements to your approach.
            – Stéphane Chazelas
            Sep 3 at 14:00

















          • Is the setopt extendedglob locally scoped? If not, can it not be locally scoped? And why does the history expansion bother you enough to warrant disabling? Any specific cons?
            – HappyFace
            Sep 3 at 11:08






          • 1




            @HappyFace, my main concern is unwanted expansions of !s. emulate -L resets the options to a sane default and makes options (including ones set later on) locally scoped. If you want extendedglob set globally (which I'd recommend for interactive use), just add setopt extendedglob to your ~/.zshrc.
            – Stéphane Chazelas
            Sep 3 at 11:10











          • BTW, I don't understand why eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' is failing. Shouldn't evaling a command be just like typing it? and thank you, things finally worked out. :D
            – HappyFace
            Sep 3 at 13:43






          • 1




            @HappyFace, history expansion is only done upon input lines coming from zle. Think of the history number for instance. It wouldn't make sense in eval
            – Stéphane Chazelas
            Sep 3 at 13:49







          • 1




            @HappyFace, see also the edit for possible improvements to your approach.
            – Stéphane Chazelas
            Sep 3 at 14:00
















          Is the setopt extendedglob locally scoped? If not, can it not be locally scoped? And why does the history expansion bother you enough to warrant disabling? Any specific cons?
          – HappyFace
          Sep 3 at 11:08




          Is the setopt extendedglob locally scoped? If not, can it not be locally scoped? And why does the history expansion bother you enough to warrant disabling? Any specific cons?
          – HappyFace
          Sep 3 at 11:08




          1




          1




          @HappyFace, my main concern is unwanted expansions of !s. emulate -L resets the options to a sane default and makes options (including ones set later on) locally scoped. If you want extendedglob set globally (which I'd recommend for interactive use), just add setopt extendedglob to your ~/.zshrc.
          – Stéphane Chazelas
          Sep 3 at 11:10





          @HappyFace, my main concern is unwanted expansions of !s. emulate -L resets the options to a sane default and makes options (including ones set later on) locally scoped. If you want extendedglob set globally (which I'd recommend for interactive use), just add setopt extendedglob to your ~/.zshrc.
          – Stéphane Chazelas
          Sep 3 at 11:10













          BTW, I don't understand why eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' is failing. Shouldn't evaling a command be just like typing it? and thank you, things finally worked out. :D
          – HappyFace
          Sep 3 at 13:43




          BTW, I don't understand why eval '^(#b)E(?)^E$(l:2::0:)$((match[1]+1))' is failing. Shouldn't evaling a command be just like typing it? and thank you, things finally worked out. :D
          – HappyFace
          Sep 3 at 13:43




          1




          1




          @HappyFace, history expansion is only done upon input lines coming from zle. Think of the history number for instance. It wouldn't make sense in eval
          – Stéphane Chazelas
          Sep 3 at 13:49





          @HappyFace, history expansion is only done upon input lines coming from zle. Think of the history number for instance. It wouldn't make sense in eval
          – Stéphane Chazelas
          Sep 3 at 13:49





          1




          1




          @HappyFace, see also the edit for possible improvements to your approach.
          – Stéphane Chazelas
          Sep 3 at 14:00





          @HappyFace, see also the edit for possible improvements to your approach.
          – Stéphane Chazelas
          Sep 3 at 14:00


















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f466540%2fhow-can-i-alias-a-history-expansion-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?

          Bahrain

          Postfix configuration issue with fips on centos 7; mailgun relay