我如何始终忽略 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
- 当我键入
cd <TAB>
时,我得到一个只有 bar
和 zsh
的菜单。这太棒了。
- 当我删除
zsh
并执行 cd <TAB>
时,bar
完成但没有显示菜单。也很棒
- 但是当我也删除
bar
并且我删除 cd <TAB>
时,foo
就完成了。我不希望发生这种情况。
- 重新开始,但是从父目录开始并执行
cd <TAB>
以完成父目录,然后 cd <TAB>
我看到 foo
或完成所有三种情况。
有没有办法完全忽略 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
在空目录中包含以下内容:
$ zsh -d -f -i
% autoload -Uz compinit && compinit
% zstyle ':completion:*:*:cd:*:*' ignored-patterns foo
% mkdir foo
% mkdir bar
% mkdir zsh
- 当我键入
cd <TAB>
时,我得到一个只有bar
和zsh
的菜单。这太棒了。 - 当我删除
zsh
并执行cd <TAB>
时,bar
完成但没有显示菜单。也很棒 - 但是当我也删除
bar
并且我删除cd <TAB>
时,foo
就完成了。我不希望发生这种情况。 - 重新开始,但是从父目录开始并执行
cd <TAB>
以完成父目录,然后cd <TAB>
我看到foo
或完成所有三种情况。
有没有办法完全忽略 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 noignored-patterns
style were set. [...] Thesingle-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