使用下面的代码,一切都按预期工作:
当我写作
c
并按下TAB
按键时,我会按正确的顺序得到适当的补全。当我书写
./
并按下TAB
按键时,我会得到标签的补全executables
。
# Always use menu selection when using unambiguous completions.
zstyle ':completion:*:*:*:*:*' menu 'select'
# Show only completions.
zstyle ':completion:*:*:*:*:*' completer _complete
# Group name becomes the name of the matching tag.
zstyle ':completion:*:*:*:*:*' group-name ''
# Configure the order of tag matching as well as their descriptions.
zstyle -e ':completion:*:*:-command-:*:*' tag-order '
reply=(
"executables:Executables:Executables
builtins:Builtins:Builtins
commands:Commands:Commands
aliases:Aliases:Aliases
functions:Functions:Functions
parameters:Variables:Variables
reserved-words:Keywords:Keywords"
)
'
# Configure the order in which completion groups will be shown.
zstyle ':completion:*:*:-command-:*:*' group-order 'Executables' 'Builtins' 'Commands' 'Aliases' 'Functions' 'Variables' 'Keywords'
# Configure the format for each group/tag description.
zstyle ':completion:*:*:*:*:descriptions' format $'%{\e[0;38;2;0;0;0;48;2;200;150;0m%} %d %{\e[0;38;2;200;150;0;48;2;0;0;0m%}%{\e[0m%}'
# Initialize completion system.
autoload -Uz compinit && compinit
但是,如果我指定的标签未产生任何补全,则也会尝试其他标签。
证明这一点的最简单方法是将functions
标签从tag-order
样式中排除:
zstyle -e ':completion:*:*:-command-:*:*' tag-order '
reply=(
"executables:Executables:Executables
builtins:Builtins:Builtins
commands:Commands:Commands
aliases:Aliases:Aliases
parameters:Variables:Variables
reserved-words:Keywords:Keywords"
)
'
然后创建两个具有唯一前缀的函数:
function unique_prefix_A() { }
function unique_prefix_B() { }
现在,当我编写unique_prefix_
并按下TAB
键时,我不希望看到任何内容,因为functions
标签已从tag-order
.
但是,由于Executables
、Builtins
、Commands
、Aliases
、Variables
和Keywords
没有提供任何补全,zsh
因此默认情况下会尝试匹配其他标签(即排除的标签functions
),以尝试给出任何补全。
因此,将建议 unique_prefix_A
以下功能:unique_prefix_B
我不喜欢这种行为,并且希望将搜索限制为仅搜索我明确指定的标签。
该手册用于标签顺序style 提出了一个简单的解决方案:
- 如果任何值仅包含连字符,则仅生成其他值中指定的标签。通常,如果指定的标签未能生成任何匹配项,则最后尝试所有未显式选择的标签。这意味着仅由单个连字符组成的单个值将关闭完成功能。
应用解决方案:
# Configure the order of tag matching as well as their descriptions.
zstyle -e ':completion:*:*:-command-:*:*' tag-order '
reply=(
"executables:Executables:Executables
builtins:Builtins:Builtins
commands:Commands:Commands
aliases:Aliases:Aliases
functions:Functions:Functions
parameters:Variables:Variables
reserved-words:Keywords:Keywords"
"-"
)
'
当前的行为是:
当我写作
c
并按下TAB
按键时,我会按正确的顺序得到适当的补全。)
当我写下
./
并按下TAB
键时,我什么也得不到。
为什么现在只有executables
标签不起作用?
我该如何修复它并获得所需的行为?
答案1
在这种情况下,标签executable
会调用。_files -g '*(-*)
然后_files
调用 more _tags
,因此有必要指定这些完成函数中隐式指定的标签, 也。
zstyle -e ':completion:*:*:-command-:*:*' tag-order '
reply=(
"executables:Executables:Executables
builtins:Builtins:Builtins
commands:Commands:Commands
aliases:Aliases:Aliases
functions:Functions:Functions
parameters:Variables:Variables
reserved-words:Keywords:Keywords
globbed-files directories"
"-"
)
'
在这种情况下添加globbed-files
and会很有用:directories
% ls -al
total 80
drwxr-xr-x 3 t t 4096 May 18 08:27 .
drwxrwxrwt 16 root root 69632 May 18 15:27 ..
drwxr-xr-x 2 t t 4096 May 18 08:27 directory
-rwxr-xr-x 1 t t 0 May 18 08:27 executable-file
-rw-r--r-- 1 t t 0 May 18 08:27 test
% ./<TAB>
Executables
directory/ executable-file*
但在上述设置中,目录和本地可执行文件将进入同一“可执行文件”组。如果我们想让“目录”进入其他组,我们可以指定file-patterns
直接并像这样使用它:
zstyle ':completion:*:*:-command-:*:*' file-patterns \
'*(#q-*):executables:Executables *(-/):directories:Directories'
zstyle -e ':completion:*:*:-command-:*:*' tag-order '
reply=(
"executables:Executables:Executables
builtins:Builtins:Builtins
commands:Commands:Commands
aliases:Aliases:Aliases
functions:Functions:Functions
parameters:Variables:Variables
reserved-words:Keywords:Keywords
directories:Directories"
-
)
'
在下面的示例中,“目录”和“可执行文件”位于单独的组中:
% ls -al
total 80
drwxr-xr-x 3 t t 4096 May 18 08:27 .
drwxrwxrwt 15 root root 69632 May 18 15:24 ..
drwxr-xr-x 2 t t 4096 May 18 08:27 directory
-rwxr-xr-x 1 t t 0 May 18 08:27 executable-file
-rw-r--r-- 1 t t 0 May 18 08:27 test
% ./<TAB>
Executables
executable-file*
Directories
directory/
下面是生成的最小 .zshrc 示例:
autoload -Uz compinit && compinit
zstyle ':completion:*:*:*:*:*' group-name ''
zstyle ':completion:*:descriptions' format '%B%F{black}%d%f%b'
zstyle ':completion:*:*:*:*:*' menu 'select'
# This comment out block is just for a reminder of my answer's first half.
# zstyle -e ':completion:*:*:-command-:*:*' tag-order '
# reply=(
# "executables:Executables:Executables
# builtins:Builtins:Builtins
# commands:Commands:Commands
# aliases:Aliases:Aliases
# functions:Functions:Functions
# parameters:Variables:Variables
# reserved-words:Keywords:Keywords
# globbed-files directories"
# "-"
# )
# '
zstyle ':completion:*:*:-command-:*:*' file-patterns \
'*(#q-*):executables:Executables *(-/):directories:Directories'
zstyle -e ':completion:*:*:-command-:*:*' tag-order '
reply=(
"executables:Executables:Executables
builtins:Builtins:Builtins
commands:Commands:Commands
aliases:Aliases:Aliases
functions:Functions:Functions
parameters:Variables:Variables
reserved-words:Keywords:Keywords
directories:Directories"
-
)
'
更新:注释掉不必要的块。