4

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?

1

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

0

You need to do two things. (Note: This is not speculation. I’ve tested it and it works.)

First, you need define file-patterns for mpv in your .zshrc:

zstyle ':completion:*:mpv:*' file-patterns \
        '%p.(mp3|mp4|mpg|avi)(#q^-/):globbed-files:"media file"' '*(-/):directories:directory' '*(#q^-/):all-files:file'

This tells Zsh's completion system that, on the first try, complete only files ending in the extensions specified (with the caption "media file", if you have group-name enabled). Only if that fails, will it offer directories. And only if that fails, too, offer any other kinds of files. ((#q^-/) matches only non-directories, whereas (-/) matches only directories.)

But that's not enough, because the completions supplied with mpv (and you did install those, right?), also try to offer URLs as completions. To avoid seeing those when there are media files available, add the following:

zstyle ':completion:*:mpv:*' tag-order 'files'

This says that for the command mpv, it should first try to offer files (which includes everything under file-patterns above) and only when that fails, offer other things.

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.