Lord Bo
Lord Bo

Reputation: 3298

zsh completion script with globbing

  1. I'd like to write a zsh-completion script, which completes with files in a directory different from the current working directory. (I can archieve this, see below.)
  2. Additionally the user shall be able to use globbing on the completed command. How to archieve this?

Example:

Let's assume we want to complete a command called rumple. rumple takes a list of files from ~/rumple as argument (possibly nested directories/files). A valid invocation may be:

rumple pumple romple/pample

Which means rumple shall effectively work on ~/rumple/pumple and ~/rumple/romple/pample.

Now the zsh-completion script _rumple for rumple could look like:

#compdef rumple

_path_files -W ~/rumple

This completes the first part of the task: rumple p<TAB> will complete to rumple pumple (assuming there are not other files in ~/rumple starting with 'p'). However the second part is not working. rumple **/p* should complete to rumple pumple romple/pample, even if the current working directory is not ~/rumple.

Upvotes: 2

Views: 335

Answers (1)

okapi
okapi

Reputation: 1460

Normally you would leave it to the user to configure this sort of behaviour via the _match completer or glob_complete option.

But you can force it to some extent as follows:

#compdef rumple
local a=( ~/rumple/**/* )
compstate[pattern_match]='*'
compadd ${a#$HOME/rumple/}

Upvotes: 1

Related Questions