9

What is the difference between autoload -U and plain autoload?

For instance, here it is recommended to run:

autoload -U run-help
autoload run-help-git
autoload run-help-svn
autoload run-help-svk
unalias run-help
alias help=run-help

Why is -U only in the first line?

1 Answer 1

10

Yes, you do see the recommendation for -U often, usually paired with -z. It’s not documented in the run-help for autoload, but there is a section titled “AUTOLOADING FUNCTIONS” in the manpage for zshmisc.

There it states:

The usual alias expansion during reading will be suppressed if the autoload builtin or its equivalent is given the option -U. This is recommended for the use of functions supplied with the zsh distribution. Note that for functions precompiled with the zcompile builtin command the flag -U must be provided when the .zwc file is created, as the corresponding information is compiled into the latter.

I read that as “disregard aliases”. The -z seems to be to avoid Ksh-isms. I just memorize -Uz and usually add them to any autoload. Maybe a worthwhile alias: alias al=’autoload -Uz’.

See also: https://stackoverflow.com/questions/12570749/zsh-completion-difference

5
  • Could you also add some explanation about the option -z as well? I barely find any reference on the Internet about what's going on related to "avoid Ksh-isms".
    – Niing
    Mar 16 at 8:43
  • 2
    @Niing the -z option loads functions as if the KSH_FUNCTIONS option were unset, and the -k option loads functions as if it were set, regardless of whether the option is set. That is, -z disables "Ksh-style" autoloading, and -k enables it. The difference between these two is described in manual section 9.1 - "Autoloading Functions". Ksh-style basically just sources the file, while Zsh-style has some extra behavior that allows the function definition file to also be used as a standalone executable script. Mar 16 at 14:37
  • 2
    The autoload builtin itself and its flags are documented in section 17 - "Shell Builtin Commands", and the KSH_AUTOLOAD option is documented in section 16.2.10 - "Shell Emulation. Mar 16 at 14:37
  • 1
    @shadowtalker: Wow thank you! I just realized that you're the original poster. Sincerely appreciate all your help!
    – Niing
    Mar 17 at 1:13
  • 1
    @Niing I've learned a lot since I first asked this question! Interestingly, I still don't have an answer to why -U is only used in the first invocation and not the others. I've been using -Uz by default for all of my own functions, because I never want alias expansion during evaluation and I always write Zsh-style function files. KSH_FUNCTIONS is disabled by default and I don't enable it in my own config, so -z is probably redundant, but it can't hurt either. Mar 17 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .