3

In my .zshrc, I have menu select disabled globally, except for mpv/mplayer:

zstyle ':completion:*' menu select=0
zstyle ':completion::complete:mpv:*' menu yes select

This works as expected, but I would like to modify the rule so that when menu is invoked, mpv only shows media files, ie 'mp3|mp4|mpg|avi|...'

I tried to change * to *.mp3 to test it, but that does not work.

How do I have to modify my rule?

0

zstyle completion settings affect how completions are displayed, and can tune how completions are generated, but does not determine the code to generate completions. That code is set with compdef, compctl or equivalent methods.

If you just want to specify media files as completions for mpv, call compctl in your .zshrc:

compctl -/ -g '*.(#i)(mp3|mp4|mpg|avi)' mpv

The -/ option says to complete directories in addition to other completions (otherwise you wouldn't be able to reach files in subdirectories). The -g option specifies a glob pattern to select files for completion. (#i) causes a case-insensitive match.

For a more complex selection of completions, write a function (out of scope for this answer) and associate it with the command with compdef or through autoload magic. This function can query zstyle styles if it wants to tune how it generates completions.

0

We could specify ignored-patterns with an extra zstyle line like this:

# append below line
zstyle ':completion::complete:mpv:*:*files' ignored-patterns '^*.(#i)(mp3|mp4|mpg|avi)'

PS: ^ is a "glob operator" such that ^x matches anything except the pattern x, so above zstyle roughly means that please teach zsh to ignore non-media-ish files for mpv command.

ignored-patterns

A list of patterns; any trial completion matching one of the patterns will be excluded from consideration. The _ignored completer can appear in the list of completers to restore the ignored matches. This is a more configurable version of the shell parameter $fignore.

Note that the EXTENDED_GLOB option is set during the execution of completion functions, so the characters ‘#’, ‘~’ and ‘^’ have special meanings in the patterns.

--- zshcompsys(1), zsh completion system, Standard styles

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.