Zsh: How to Exclude Multiple Files from Sourcing

wildcardszsh

I have setopt extended_glob, I want to exclude all files under ~/.config/zsh that either start with _ or filename is completion.zsh or options.zsh

I have this command:

for f (${${(%):-%x}:P:h:h}/**/([^_|^completion|^options])*.zsh) print $f

It does the job but above command also exclude lazyload.zsh, why above command exclude this pattern? I expected it will print out lazyload.zsh.

Best Answer

Just:

${${(%):-%x}:P:h:h}/**/(^(_|completion|options)*).zsh

^ is the extendedglob negation operator.

Remember [anything] only ever matches one character¹.

[a|b] matches a, | or b, [^a|b] matches any character but a, | or b.

So your pattern excludes lazyload.zsh because l is in the list of character excluded by your [^...] single-character-matching operator.


¹ (in zsh; in some other shells, it may also match multi-character collating elements in some locales; zsh's [...] can also match bytes not forming parts of valid characters, YMMV with other shells)