我如何始终忽略 ZSH 完成中的模式?

How do I always ignore a pattern in ZSH completion?

在空目录中包含以下内容:

$ zsh -d -f -i

% autoload -Uz compinit && compinit
% zstyle ':completion:*:*:cd:*:*' ignored-patterns foo
% mkdir foo
% mkdir bar
% mkdir zsh

有没有办法完全忽略 foo,这样我就再也看不到它,也永远不会在同一目录和任何其他目录中完成它?

编辑:

我发现使用 zstyle ':completion:*:*:cd:*:*' ignored-patterns '**/foo' 从父目录中看到被忽略的模式的问题消失了,但是当没有其他选择时被忽略的模式仍然完成。所以有了这个:

$ zsh -d -f -i

% autoload -Uz compinit && compinit
% zstyle ':completion:*:*:cd:*:*' ignored-patterns '**/foo'
% mkdir foo

并键入 cd <Tab> 仍然会完成 foo。有没有办法在这种情况下不完成?

此行为的原因

zsh补全系统有多个补全功能。这些通过以下方式启用:

zstyle ':completion:*' completer <list of completers>

默认值为 _complete _ignored (see)。

这意味着首先尝试常规完成,如果没有产生完成,则会尝试特殊的 _ignored 完成器。 _ignored 完成器会忽略您定义的 ignored-patterns 样式,因此会找到 foo 匹配项。

来自zsh documentationon on _ignored:

The ignored-patterns style can be set to a list of patterns which are compared against possible completions; matching ones are removed. With this completer those matches can be reinstated, as if no ignored-patterns style were set. [...] The single-ignored style is also available as described above.

一个解决方案

从完成者列表中删除 _ignored

您可以通过zstyle -L '*' completer显示当前列表。 如果这是空的,它仍然是默认值,您可以通过以下方式禁用 _ignored

zstyle ':completion:*' completer _complete

一种解决方案

来自documentation entry on single-ignored(在上面的引用中提到):

This is used by the _ignored completer when there is only one match. If its value is ‘show’, the single match will be displayed but not inserted. If the value is ‘menu’, then the single match and the original string are both added as matches and menu completion is started, making it easy to select either of them.

因此,如果您将其设置为 show(或 menu)(通过 zstyle ':completion:*' single-ignored show),则它不会立即完成,只会显示在选项卡完成菜单中。 这意味着您可以忽略它并继续输入。

附录

(据我所知)不可能仅针对 cd(比如 zstyle ':completion:*:*:cd:*:*' completer ...)禁用完成程序,因为它们是在完成过程的最开始就确定的.

还有一种方法可以使用 file-patterns style^ glob 模式来忽略某些 file/directory 模式,但这种风格似乎没有与 [=32= 一起使用】 完成。但是例如对于 ls 这应该可以解决问题:

zstyle ':completion:*:*:ls:*:*' file-patterns '^foo|^**/foo:directories'

完成时的 zsh 指南也是一个很好的资源: http://zsh.sourceforge.net/Guide/zshguide06.html