Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Matching anywhere in a full path



On Mon, Apr 13, 2015 at 10:06 PM, Jesper Nygårds
<jesper.nygards@xxxxxxxxx> wrote:
> I am working on an idea with a completion function that generates file
> paths, absolute or relative. Below is a simplified version of what I am
> trying to do. I have replaced what I'm really using to generate the paths
> with a call to "find", just to make it simple to see what I am stumbling on.
>
> Here's how I've set up the function:
>
> _gen-result() {
>     local -a hlist
>     hlist=("${(@f)$(find /etc/ -type f)}") # This is just a stand-in for my
> real function
>     compadd -- $hlist
> }
>
> zle -C gen-comp menu-complete _gen-result
> bindkey '\ee' gen-comp
>
> I works well. Typing "ls <\ee>" I get the files found below /etc.
>
> Here's what I don't know how to solve: I would like to type some string on
> the command line, and I want to have that string working as a filter for
> the suggestions, matching anywhere in the path. Say I have "/etc/foo",
> "/etc/bar" and /etc/baz/foo.txt", and I type "ls foo <\ee>", I only want
> "/etc/foo" and "/etc/baz/foo.txt" to be suggested. In other words, I want
> the completion to match against the string anywhere in the full path.
>
> In this particular example, I realize I could pass the filter string as a
> restriction to the find command, and I could also filter the array "hlist"
> after the paths are generated, but that is not so simple in my real
> command. Is there a way to specify in a more "zsh like" way that when using
> _gen-result, the whole path should be examined for a match?

If you have an array foo, and a string bar, you can return all
elements that contain $bar by doing
${(M)foo:#*$bar*}
(Without the (M), the matching elements will be removed).
Eg, compadd -- ${hlist:#*$filter*}
If this is already what you mean by "that is not so simple in my real
command", then your example is too simplified :).

-- 
Mikael Magnusson



Messages sorted by: Reverse Date, Date, Thread, Author