Why doesn't `autoload -U` suppress the expansion of suffix aliases, in zsh?

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











up vote
0
down vote

favorite












I read from this answer that the more reliable way to test whether a command exist is to use $ command -v:



if command -v given-command > /dev/null 2>&1; then
echo given-command is available
else
echo given-command is not available
fi


I have used this command to get a listing of all valid commands in my $PATH:



get_all_cmds() sort 


It works, but in the pager I can read this command:



/home/user/bin/README.md


This is not the path to a command, but a readme file that I wrote in my bin/ directory to document why some scripts were there.



It turns out that $ command -v /home/user/bin/README.md returns vim, and more importantly exits with the code 0. This is due to this suffix alias which I wrote in ~/.zshrc:



alias -s md,markdown,txt,html,css=vim


I think that, because of it, when $ command tries to execute /home/user/bin/README.md, instead of failing, it expands the suffix alias into vim /home/user/bin/README.md, which is a valid command.



I found a solution here. It temporarily removes all aliases. Adapted to my issue with suffix aliases, it gives something like:



 # save suffix aliases
local aliases_save
aliases_save=$(alias -L -s)

# remove suffix aliases
unalias -s -m '*'

# do what you have to do
....

# restore suffix aliases
eval "$aliases_save"


It seems to work, but I wondered whether a simpler solution existed, which would also protect the function from regular aliases. In $ man zshbuiltins, I read this about the -U option which can be passed to the autoload builtin:




With the -U flag, alias expansion is suppressed when the function is loaded.




It seems to be what I wanted. I made the get_all_cmds function autoloadable so that I could load it with autoload -U thinking that the -U option would suppress any alias expansion.
To do so, in ~/.zshrc, I have added the directory ~/.zsh/my_func/ inside $fpath:



fpath=( $HOME/.zsh/my_func/ $fpath )


Then, in ~/.zsh/my_func/get_all_cmds, I wrote the body of the function:



emulate -L zsh
unsetopt nomatch

for dir in $(tr ':' ' ' <<<"$PATH"); do
for cmd in $dir/*; do
if command -v "$cmd" >/dev/null 2>&1; then
echo "$cmd"
fi
done
done | sort | less


Finally, in ~/.zshrc, I made the function autoloadable:



autoload -Uz get_all_cmds


But when I invoke the get_all_cmds function on the shell command line, I can still find /home/user/bin/README.md in the pager.



I thought that maybe the code contained a syntax error, so I tried with a simpler code:



fpath=( ~/.zsh/my_func/ $fpath ) && 
mkdir -p ~/.zsh/my_func/ &&
echo 'ls' >~/.zsh/my_func/my_colored_ls &&
alias ls='ls --color' &&
autoload -Uz my_colored_ls &&
my_colored_ls


Here, the code works as expected, because the output printed by $ ls is not colorized, even though the regular alias ls='ls --color' is defined.
On the other hand, if I restart a new shell and I remove the -U option which was passed to autoaload:



fpath=( ~/.zsh/my_func/ $fpath ) && 
mkdir -p ~/.zsh/my_func/ &&
echo 'ls' >~/.zsh/my_func/my_colored_ls &&
alias ls='ls --color' &&
autoload -z my_colored_ls &&
my_colored_ls


This time, the output of $ ls is colorized.
So, the syntax of the commands seems correct since, here, -U is able to suppress the expansion of alias ls='ls --color'.



I also tried with a global alias (alias -g L=' | less'):



fpath=( ~/.zsh/my_func/ $fpath ) && 
mkdir -p ~/.zsh/my_func/ &&
echo 'echo "should be read in pager" L' >~/.zsh/my_func/my_test &&
alias -g L='| less' &&
autoload -Uz my_test &&
my_test


And again, the -U option correctly suppresses the expansion of the global alias, because instead of reading should be read in pager inside the $ less pager, should be read in pager L is printed on the terminal.



Why does the -U option of the autoload zsh builtin suppresses the expansion of regular and global aliases, but not the expansion of suffix aliases?









share

























    up vote
    0
    down vote

    favorite












    I read from this answer that the more reliable way to test whether a command exist is to use $ command -v:



    if command -v given-command > /dev/null 2>&1; then
    echo given-command is available
    else
    echo given-command is not available
    fi


    I have used this command to get a listing of all valid commands in my $PATH:



    get_all_cmds() sort 


    It works, but in the pager I can read this command:



    /home/user/bin/README.md


    This is not the path to a command, but a readme file that I wrote in my bin/ directory to document why some scripts were there.



    It turns out that $ command -v /home/user/bin/README.md returns vim, and more importantly exits with the code 0. This is due to this suffix alias which I wrote in ~/.zshrc:



    alias -s md,markdown,txt,html,css=vim


    I think that, because of it, when $ command tries to execute /home/user/bin/README.md, instead of failing, it expands the suffix alias into vim /home/user/bin/README.md, which is a valid command.



    I found a solution here. It temporarily removes all aliases. Adapted to my issue with suffix aliases, it gives something like:



     # save suffix aliases
    local aliases_save
    aliases_save=$(alias -L -s)

    # remove suffix aliases
    unalias -s -m '*'

    # do what you have to do
    ....

    # restore suffix aliases
    eval "$aliases_save"


    It seems to work, but I wondered whether a simpler solution existed, which would also protect the function from regular aliases. In $ man zshbuiltins, I read this about the -U option which can be passed to the autoload builtin:




    With the -U flag, alias expansion is suppressed when the function is loaded.




    It seems to be what I wanted. I made the get_all_cmds function autoloadable so that I could load it with autoload -U thinking that the -U option would suppress any alias expansion.
    To do so, in ~/.zshrc, I have added the directory ~/.zsh/my_func/ inside $fpath:



    fpath=( $HOME/.zsh/my_func/ $fpath )


    Then, in ~/.zsh/my_func/get_all_cmds, I wrote the body of the function:



    emulate -L zsh
    unsetopt nomatch

    for dir in $(tr ':' ' ' <<<"$PATH"); do
    for cmd in $dir/*; do
    if command -v "$cmd" >/dev/null 2>&1; then
    echo "$cmd"
    fi
    done
    done | sort | less


    Finally, in ~/.zshrc, I made the function autoloadable:



    autoload -Uz get_all_cmds


    But when I invoke the get_all_cmds function on the shell command line, I can still find /home/user/bin/README.md in the pager.



    I thought that maybe the code contained a syntax error, so I tried with a simpler code:



    fpath=( ~/.zsh/my_func/ $fpath ) && 
    mkdir -p ~/.zsh/my_func/ &&
    echo 'ls' >~/.zsh/my_func/my_colored_ls &&
    alias ls='ls --color' &&
    autoload -Uz my_colored_ls &&
    my_colored_ls


    Here, the code works as expected, because the output printed by $ ls is not colorized, even though the regular alias ls='ls --color' is defined.
    On the other hand, if I restart a new shell and I remove the -U option which was passed to autoaload:



    fpath=( ~/.zsh/my_func/ $fpath ) && 
    mkdir -p ~/.zsh/my_func/ &&
    echo 'ls' >~/.zsh/my_func/my_colored_ls &&
    alias ls='ls --color' &&
    autoload -z my_colored_ls &&
    my_colored_ls


    This time, the output of $ ls is colorized.
    So, the syntax of the commands seems correct since, here, -U is able to suppress the expansion of alias ls='ls --color'.



    I also tried with a global alias (alias -g L=' | less'):



    fpath=( ~/.zsh/my_func/ $fpath ) && 
    mkdir -p ~/.zsh/my_func/ &&
    echo 'echo "should be read in pager" L' >~/.zsh/my_func/my_test &&
    alias -g L='| less' &&
    autoload -Uz my_test &&
    my_test


    And again, the -U option correctly suppresses the expansion of the global alias, because instead of reading should be read in pager inside the $ less pager, should be read in pager L is printed on the terminal.



    Why does the -U option of the autoload zsh builtin suppresses the expansion of regular and global aliases, but not the expansion of suffix aliases?









    share























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I read from this answer that the more reliable way to test whether a command exist is to use $ command -v:



      if command -v given-command > /dev/null 2>&1; then
      echo given-command is available
      else
      echo given-command is not available
      fi


      I have used this command to get a listing of all valid commands in my $PATH:



      get_all_cmds() sort 


      It works, but in the pager I can read this command:



      /home/user/bin/README.md


      This is not the path to a command, but a readme file that I wrote in my bin/ directory to document why some scripts were there.



      It turns out that $ command -v /home/user/bin/README.md returns vim, and more importantly exits with the code 0. This is due to this suffix alias which I wrote in ~/.zshrc:



      alias -s md,markdown,txt,html,css=vim


      I think that, because of it, when $ command tries to execute /home/user/bin/README.md, instead of failing, it expands the suffix alias into vim /home/user/bin/README.md, which is a valid command.



      I found a solution here. It temporarily removes all aliases. Adapted to my issue with suffix aliases, it gives something like:



       # save suffix aliases
      local aliases_save
      aliases_save=$(alias -L -s)

      # remove suffix aliases
      unalias -s -m '*'

      # do what you have to do
      ....

      # restore suffix aliases
      eval "$aliases_save"


      It seems to work, but I wondered whether a simpler solution existed, which would also protect the function from regular aliases. In $ man zshbuiltins, I read this about the -U option which can be passed to the autoload builtin:




      With the -U flag, alias expansion is suppressed when the function is loaded.




      It seems to be what I wanted. I made the get_all_cmds function autoloadable so that I could load it with autoload -U thinking that the -U option would suppress any alias expansion.
      To do so, in ~/.zshrc, I have added the directory ~/.zsh/my_func/ inside $fpath:



      fpath=( $HOME/.zsh/my_func/ $fpath )


      Then, in ~/.zsh/my_func/get_all_cmds, I wrote the body of the function:



      emulate -L zsh
      unsetopt nomatch

      for dir in $(tr ':' ' ' <<<"$PATH"); do
      for cmd in $dir/*; do
      if command -v "$cmd" >/dev/null 2>&1; then
      echo "$cmd"
      fi
      done
      done | sort | less


      Finally, in ~/.zshrc, I made the function autoloadable:



      autoload -Uz get_all_cmds


      But when I invoke the get_all_cmds function on the shell command line, I can still find /home/user/bin/README.md in the pager.



      I thought that maybe the code contained a syntax error, so I tried with a simpler code:



      fpath=( ~/.zsh/my_func/ $fpath ) && 
      mkdir -p ~/.zsh/my_func/ &&
      echo 'ls' >~/.zsh/my_func/my_colored_ls &&
      alias ls='ls --color' &&
      autoload -Uz my_colored_ls &&
      my_colored_ls


      Here, the code works as expected, because the output printed by $ ls is not colorized, even though the regular alias ls='ls --color' is defined.
      On the other hand, if I restart a new shell and I remove the -U option which was passed to autoaload:



      fpath=( ~/.zsh/my_func/ $fpath ) && 
      mkdir -p ~/.zsh/my_func/ &&
      echo 'ls' >~/.zsh/my_func/my_colored_ls &&
      alias ls='ls --color' &&
      autoload -z my_colored_ls &&
      my_colored_ls


      This time, the output of $ ls is colorized.
      So, the syntax of the commands seems correct since, here, -U is able to suppress the expansion of alias ls='ls --color'.



      I also tried with a global alias (alias -g L=' | less'):



      fpath=( ~/.zsh/my_func/ $fpath ) && 
      mkdir -p ~/.zsh/my_func/ &&
      echo 'echo "should be read in pager" L' >~/.zsh/my_func/my_test &&
      alias -g L='| less' &&
      autoload -Uz my_test &&
      my_test


      And again, the -U option correctly suppresses the expansion of the global alias, because instead of reading should be read in pager inside the $ less pager, should be read in pager L is printed on the terminal.



      Why does the -U option of the autoload zsh builtin suppresses the expansion of regular and global aliases, but not the expansion of suffix aliases?









      share













      I read from this answer that the more reliable way to test whether a command exist is to use $ command -v:



      if command -v given-command > /dev/null 2>&1; then
      echo given-command is available
      else
      echo given-command is not available
      fi


      I have used this command to get a listing of all valid commands in my $PATH:



      get_all_cmds() sort 


      It works, but in the pager I can read this command:



      /home/user/bin/README.md


      This is not the path to a command, but a readme file that I wrote in my bin/ directory to document why some scripts were there.



      It turns out that $ command -v /home/user/bin/README.md returns vim, and more importantly exits with the code 0. This is due to this suffix alias which I wrote in ~/.zshrc:



      alias -s md,markdown,txt,html,css=vim


      I think that, because of it, when $ command tries to execute /home/user/bin/README.md, instead of failing, it expands the suffix alias into vim /home/user/bin/README.md, which is a valid command.



      I found a solution here. It temporarily removes all aliases. Adapted to my issue with suffix aliases, it gives something like:



       # save suffix aliases
      local aliases_save
      aliases_save=$(alias -L -s)

      # remove suffix aliases
      unalias -s -m '*'

      # do what you have to do
      ....

      # restore suffix aliases
      eval "$aliases_save"


      It seems to work, but I wondered whether a simpler solution existed, which would also protect the function from regular aliases. In $ man zshbuiltins, I read this about the -U option which can be passed to the autoload builtin:




      With the -U flag, alias expansion is suppressed when the function is loaded.




      It seems to be what I wanted. I made the get_all_cmds function autoloadable so that I could load it with autoload -U thinking that the -U option would suppress any alias expansion.
      To do so, in ~/.zshrc, I have added the directory ~/.zsh/my_func/ inside $fpath:



      fpath=( $HOME/.zsh/my_func/ $fpath )


      Then, in ~/.zsh/my_func/get_all_cmds, I wrote the body of the function:



      emulate -L zsh
      unsetopt nomatch

      for dir in $(tr ':' ' ' <<<"$PATH"); do
      for cmd in $dir/*; do
      if command -v "$cmd" >/dev/null 2>&1; then
      echo "$cmd"
      fi
      done
      done | sort | less


      Finally, in ~/.zshrc, I made the function autoloadable:



      autoload -Uz get_all_cmds


      But when I invoke the get_all_cmds function on the shell command line, I can still find /home/user/bin/README.md in the pager.



      I thought that maybe the code contained a syntax error, so I tried with a simpler code:



      fpath=( ~/.zsh/my_func/ $fpath ) && 
      mkdir -p ~/.zsh/my_func/ &&
      echo 'ls' >~/.zsh/my_func/my_colored_ls &&
      alias ls='ls --color' &&
      autoload -Uz my_colored_ls &&
      my_colored_ls


      Here, the code works as expected, because the output printed by $ ls is not colorized, even though the regular alias ls='ls --color' is defined.
      On the other hand, if I restart a new shell and I remove the -U option which was passed to autoaload:



      fpath=( ~/.zsh/my_func/ $fpath ) && 
      mkdir -p ~/.zsh/my_func/ &&
      echo 'ls' >~/.zsh/my_func/my_colored_ls &&
      alias ls='ls --color' &&
      autoload -z my_colored_ls &&
      my_colored_ls


      This time, the output of $ ls is colorized.
      So, the syntax of the commands seems correct since, here, -U is able to suppress the expansion of alias ls='ls --color'.



      I also tried with a global alias (alias -g L=' | less'):



      fpath=( ~/.zsh/my_func/ $fpath ) && 
      mkdir -p ~/.zsh/my_func/ &&
      echo 'echo "should be read in pager" L' >~/.zsh/my_func/my_test &&
      alias -g L='| less' &&
      autoload -Uz my_test &&
      my_test


      And again, the -U option correctly suppresses the expansion of the global alias, because instead of reading should be read in pager inside the $ less pager, should be read in pager L is printed on the terminal.



      Why does the -U option of the autoload zsh builtin suppresses the expansion of regular and global aliases, but not the expansion of suffix aliases?







      zsh





      share












      share










      share



      share










      asked 2 mins ago









      user938271

      20918




      20918

























          active

          oldest

          votes











          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: 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%2f479000%2fwhy-doesnt-autoload-u-suppress-the-expansion-of-suffix-aliases-in-zsh%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f479000%2fwhy-doesnt-autoload-u-suppress-the-expansion-of-suffix-aliases-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