Zsh completion for media player

autocompletezsh

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?

Best Answer

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.

Related Question