为什么“标签顺序”样式中的值‘-’(连字符)会阻止“可执行文件”标签的匹配,即使该标签已明确指定?

为什么“标签顺序”样式中的值‘-’(连字符)会阻止“可执行文件”标签的匹配,即使该标签已明确指定?

使用下面的代码,一切都按预期进行:

  • 当我书写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
但是,由于ExecutablesBuiltinsCommandsAliases和没有提供任何补全,因此默认情况下将尝试匹配其他标签(即排除的标签),以尝试提供任何补全。 VariablesKeywordszshfunctions

因此,将建议 以下功能unique_prefix_A和:unique_prefix_B

图像

我不喜欢这种行为并且想要将搜索限制在明确指定的标签内。

手册标签顺序风格表明了一个简单的解决方案:

- 如果任何值仅由连字符组成,则仅生成其他值中指定的标签。通常,如果指定的标签无法生成任何匹配项,则最后尝试所有未明确选择的标签。这意味着仅由单个连字符组成的单个值会关闭完成。

应用解决方案:

# 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_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以下内容会很有用: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"
            -
          )
'

更新:注释掉不必要的块。

相关内容