我致力于控制dotfile的源代码,而我正在做的事情则基于Zach Holman的dotfiles。我想像他在文件中一样获取所有zsh文件的源,但是在我将代码丢到那里之前,我想真正了解代码在做什么。我很困惑的片段是
# all of our zsh files
typeset -U config_files
config_files=($ZSH/**/*.zsh)
# load the path files
for file in ${(M)config_files:#*/path.zsh}
do
source $file
done
# load everything but the path and completion files
for file in ${${config_files:#*/path.zsh}:#*/completion.zsh}
do
source $file
done
# initialize autocomplete here, otherwise functions won't be loaded
autoload -U compinit
compinit
# load every completion after autocomplete loads
for file in ${(M)config_files:#*/completion.zsh}
do
source $file
done
unset config_files
通常我对这里发生的事情感到困惑
${(M)config_files:#*/path.zsh}
和这里
${${config_files:#*/path.zsh}:#*/completion.zsh}
那么,这意味着什么?
手册页中的PARAMETER EXPANSION
部分zshexpn
是一个很好的起点。
首先,请注意,这$config_files
是一个包含.zsh
目录下所有文件的数组$ZSH
,如第二行所示config_files=($ZSH/**/*.zsh)
。
该行中使用的以下语法${(M)config_files:#*/path.zsh}
(请注意,M
括号内称为扩展标志)如下:
${name:#pattern}
If the pattern matches the value of name, then substitute the
empty string; otherwise, just substitute the value of name.
If name is an array the matching array elements are removed
(use the `(M)' flag to remove the non-matched elements).
换句话说,有问题的for循环遍历path.zsh
$ ZSH中的所有文件。您也可以使用for file in $ZSH/**/path.zsh
,但是对$config_files
文件数组的操作要比一次又一次地递归搜索文件系统快。(还有更多的for循环,不是吗?)
有了这些知识,就很容易弄清楚${${config_files:#*/path.zsh}:#*/completion.zsh}
将要做什么。(无论如何,结果在注释中说明)。
我通常使用一些琐碎的示例来更好地了解自己:
$ array=(foo bar baz)
$ print ${array}
foo bar baz
$ print ${array:#ba*}
foo
$ print ${(M)array:#ba*}
bar baz
它比看起来容易,对吧? ;)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句