4

With the code below, everything works as expected:

  • When I write c and hit the TAB key, I get the appropriate completions in correct order.

    Image

  • When I write ./ and hit the TAB key, I get the completion for the executables tag.

    Image


# 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

However if the tags that I've specified do not produce any completion, other tags will be tried as well.

Easiest way to prove this is to exclude the functions tag from tag-order style:

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"
          )
'

and then create 2 functions with a unique prefix:

function unique_prefix_A() { }
function unique_prefix_B() { }

Now when I write unique_prefix_ and hit the TAB key, I don't expect to see anything since functions tag was removed from tag-order.
However, because Executables, Builtins, Commands, Aliases, Variables and Keywords didn't provide any completions, zsh will by default try to match other tags, namely the excluded functions tag, to try to give any completion.

Because of this, the functions unique_prefix_A and unique_prefix_B will be suggested:

Image

I don't like this behavior and want to limit the search only to tags that I have explicitly specified.

The manual for the tag-order style suggests a simple solution:

- If any value consists of only a hyphen, then only the tags specified in the other values are generated. Normally all tags not explicitly selected are tried last if the specified tags fail to generate any matches. This means that a single value consisting only of a single hyphen turns off completion.

Applying the solution:

# 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"
           "-"
          )
'

Current behavior is:

  • When I write c and hit the TAB key, I get the appropriate completions in correct order.

    Image)

  • When I write ./ and hit the TAB key, I get nothing.

    Image


Why is now only executables tag not working?

How can I fix it and get the desired behavior?

migrated from stackoverflow.com May 26 '18 at 15:22

This question came from our site for professional and enthusiast programmers.

  • Updated my answer, hope it's what you want. – Miguel Ortiz May 18 '18 at 17:37
2

The executable tag invokes _files -g '*(-*) in this case. Then _files calls more _tags, so it would be necessary to specify these implicitly specified tags in the completion functions, too.

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"
           "-"
          )
'

It would be useful to add globbed-files and directories in this case:

% 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*

But on the above setting, directories and local executable files will go into the same "Executable" group. If we would like to make "directories" go into other groups, we could specify the file-patterns directly and use it like this:

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"
            -
          )
'

In the below example, "directory" and "executable-file" are in the separated groups:

% 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/

Below is the resulted smallest .zshrc example:

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"
            -
          )
'

Updated: comment out the unecessary block.

  • 1
    Thank you for the links you provided! I've copied this answer to them. – hchbaw May 18 '18 at 23:14
  • Can you please try to explain the minimal .zshrc a bit more detailed? Of course, the part that is still unclear is why do we need to have 2 tag-order definitions. Why doesn't it work with just 1 file-patterns and 1 tag-order definition? – Iskustvo May 18 '18 at 23:48
  • Actually, I've managed to make it work with just one tag-order: 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:Directories" "-")' and then zstyle ':completion:*:*:-command-:*:*' file-patterns '*(#q-*):executables:Executables:Executables *(-/):directories:Directories:Directories'. As far as I see, this works fine, do you see any problem? – Iskustvo May 19 '18 at 0:15
  • No problem! I've updated; comment out the unnecessary block in my answer. I think It would be more clear now, thanks to your suggestion! – hchbaw May 19 '18 at 1:30
1

I think the problem isn't only the (hyphen) but how specific you are about the autocompletion rules.

I ran some tests, for example, removing 'Variables' from order tag and creating (TEST, LOLO) in the shell.

After that I've tried autocompletion expecting not to see anything, but instead being listed in "Variables" they appeared as "Parameters".

That behaviour made me think about UDEV and how certain systems require quite specific rules for certain behaviour.

Your tag matching group is ok and work as intended (in the ./ scenario), but one thing is to execute:

./<TAB>

and a complete different thing is to autocomplete something like:

unique_prefix<TAB>

So it doesn't matter how much hyphens you add on those group, it will not work because it's a different rule execution.

Instead of dealing with that I've searched for an example to avoid specifically functions and this snippet worked quite fine (check source):

One use of these features is to try one tag more than once, setting other styles differently on each attempt, but still to use all the other tags without having to repeat them all. For example, to make completion of function names in command position ignore all the completion functions starting with an underscore the first time completion is tried:

(I've changed this to match all functions):

ignored-patterns ’_*’   

for this:

ignored-patterns ’*’

In this rules:

zstyle ’:completion:*:*:-command-:*:*’ tag-order \
    ’functions:-non-comp *’ functions
zstyle ’:completion:*:functions-non-comp’ \
    ignored-patterns ’*’

And when I've tried to autocomplete: unique_prefix (nothing happened) and the ./ execution worked well.

So, the answer is: you'll need specific rules for every different scenario remembering that any pattern could be interpreted different according the rules you've defined.

  • First of all, thank you for your effort. The link for first image is invalid, but I don't think that is relevant here. The code you use and the behavior you described are exactly the same as mine in the original case or am I wrong? The reason why I didn't stop at this point is because if there are no matches for the tags that I've specified, some other completion would be offered in the list. I don't want that. Therefore, I need to find a way to add "-" in the tag-order list to remove other completions and keep only specified tags shown. – Iskustvo May 17 '18 at 20:46
  • I have updated the question, hope it will help you a bit. – Iskustvo May 17 '18 at 22:47
  • Sorry but this is not the real answer, just advice (which I do appreciate). Regarding the Variables and parameters, I feel like that's the same as my example with Functions. The completion didn't match anything specified and there was no - to stop it from trying to match anything at all so it matched the variables. The only difference is that the default description for that tag is paramters insead of Variables which I redefined. – Iskustvo May 18 '18 at 19:15
  • So you want a grouping rule for all cases, like a generic rule for every completion without having in mind the pattern in the shell, right? So In that way you'll define exactly what you want in one rule and nothing else. – Miguel Ortiz May 18 '18 at 19:45
  • Nevermind, saw the answer, I was looking how to define it like that!! :) happy you got the answer. – Miguel Ortiz May 18 '18 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.