0

I have 2 folders that contains folders

/me/foo/: 
  a/
  b/
  c/

/me/bar/:
  c/
  d/
  e/

I want to create completion function for command baz so that

$ baz [tab]

gives completion arguments as follow

a b c d e

notice the duplicate c written only once and it doesn't include the trailing slash.

I know how to do completion with _files but that only gives me file from single directory.

How do I do that?

ahmy
  • 4,095
  • 4
  • 27
  • 36

1 Answers1

0

I found this is working for me

local services
services=$(ls -d /me/foo/*/ /me/bar/*/ | xargs basename| uniq)
_alternative "dirs:some directories:($services)"
ahmy
  • 4,095
  • 4
  • 27
  • 36
  • This does not look like `services` would contain each name only once. It also depends on the implementation of `basename`, not all of which allow multiple arguments by default. I would suggest using `local -U services=((foo|bar)/*(/:t))`. With `-U` an array only keeps the first occurrence of identical values. `(foo|bar)/*` matches any child of the directories `foo` and `bar`. The qualifier `(/)` limits this to only directories and `(:t)` removes all leading path components aka `basename`. It also removes the need for any external command. – Adaephon Apr 08 '16 at 05:37
  • Hmm i get a bad pattern when trying `local -U services((foo|bar)/*(/:t)` and `echo $service` the error was `a.zsh:1: bad pattern: services((foo|bar)/*(/:t)\necho \n` where can I find the documentation of that pattern? – ahmy Apr 08 '16 at 07:50
  • It looks like the `=` is missing. But I get the the same error message (even with the `=`) when trying to run this on a older `zsh` with version 5.0.6. I currently use 5.2. It seems that the ability to typeset and assign arrays in a single command [was added in 5.1](http://zsh.sourceforge.net/releases.html). Separating it in `local -U services` and `services=(…)` should work. As for documentation: have a look at the section on [Filename Generation](http://zsh.sourceforge.net/Doc/Release/Expansion.html#Filename-Generation) in the ZSH manual. – Adaephon Apr 08 '16 at 08:51