autoload在zsh中做了什么?

Bai*_*Dev 21 zsh

我无法autoload在zsh中找到广泛使用的命令的文档.有人能用简单的英语解释吗?

更具体一点:模块的自动加载意味着什么,例如在这一行中:

autoload -Uz vcs_info

它有什么作用?


我试过autoload --help,man autoload谷歌搜索 - 没有成功.谢谢!

cda*_*rke 25

autoload功能在bash中不可用,但它位于ksh(korn shell)和zsh.上zshman zshbuiltins.

以与任何其他命令相同的方式调用函数.程序和函数之间可能存在名称冲突.什么autoload做的是,以纪念该名称作为一个函数,而不是一个外部程序.该函数必须独立于一个文件中,文件名与函数名相同.

autoload -Uz vcs_info
Run Code Online (Sandbox Code Playgroud)

-U方法标记vcs_info自动加载功能并抑制别名扩展.该-z方法使用zsh(而不是ksh)的风格.另请参见functions命令.

有关更多详细信息,请参阅http://zsh.sourceforge.net/Doc/Release/Functions.html.

  • 我仍然不知道 autoload _actually_ 是做什么的。它调用一个函数吗?与 eval 或 source 有任何关系吗?为什么我要使用 `autoload -U compinit` 而不是简单的 `compinit`?“自动加载标记”是什么意思? (6认同)
  • 上图:“自动加载的作用是将该名称标记为函数而不是外部程序。”。所以它记录了这个名字是一个函数而不是一个外部程序的事实——它不会调用它,除非使用`-X`选项,它只会在调用时影响搜索路径。如果函数名称不与程序名称冲突,则不需要它。用`f_` 之类的东西作为你的函数的前缀,你可能永远不需要它。 (2认同)
  • “在你的函数中加上 f_ 这样的前缀,你可能永远都不需要它”是不正确的;如果在 $PATH 中找不到命令,则不会自动在 $FPATH 中搜索命令。只有自动加载的名称才会以这种方式进行搜索。如果您的函数已定义,然后在同一文件中调用,则如果您将其放入 $PATH 中,它可能会作为脚本工作,但这不是同一回事。 (2认同)

Mar*_*eed 18

autoload告诉 zsh 在$FPATH/$fpath中查找包含函数定义的文件,而不是$PATH/ 中$path包含脚本(或二进制可执行文件)的文件。

脚本只是在脚本运行时执行的一系列命令。例如,假设您有一个名为 的文件hello

echo "Setting 'greeting'"
greeting='Hello'
Run Code Online (Sandbox Code Playgroud)

Scripts get their own copy of the shell process, so anything they do can't have any side effects on the calling shell environment. The assignment to greeting above will be in effect only within the script; once it exits, it won't have had any impact on your interactive shell session:

$ hello
Setting 'greeting'
$ echo $greeting

$ 
Run Code Online (Sandbox Code Playgroud)

Functions are defined once and live in the shell's memory; when you call them, they excecute inside your shell environment and can therefore have side effects:

hello() {
  echo "Setting 'greeting'"
  greeting='Hello'
}

$ hello
Setting 'greeting'
$ echo $greeting
Hello
Run Code Online (Sandbox Code Playgroud)

So you use functions when you want to modify your shell environment. The Zsh Line Editor (ZLE) also uses functions - when you bind a key to some action, that action is defined as a shell function (which has to be added to ZLE with the zle -N command.)

Now, if you have a lot of functions, then you might not want to define all of them in your .zshrc every time you start a new shell; that slows down shell startup and uses memory to store functions that you might not wind up calling during the lifetime of that shell. So you can instead put the function definitions into their own files, named after the functions they define, and put the files into directories in your $FPATH, which works like $PATH. Zsh comes with a bunch of standard functions in the default $FPATH already. But it won't know to look for a command there unless you've first told it that the command is a function. That's essentially what autoload does; it says "Hey, Zsh, this command name here is a function, so when I try to run it, go look for its definition in my FPATH instead of looking for a command in my PATH."

Worth noting is that the first time you run an autoloaded function, Zsh sources the definition file, but – unlike ksh - it does not then call the function. It's up to you to call the function inside the file after defining it so that first invocation will work. An autoloadable definition of hello might look like this:

hello() {
  echo "Setting 'greeting'"
  greeting='Hello'
}
hello "$@"
Run Code Online (Sandbox Code Playgroud)

  • 这种非常有用的解释应该出现在主流文档/指南中,因为它真正解释了这些功能背后的“原因”。恕我直言,除了面向使用的手册之外,每个功能都应该有这个面向目的的“为什么”。多谢。 (2认同)