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

Re: completion for function arguments?



On 8/8/19, TJ Luoma <luomat@xxxxxxxxx> wrote:
> Let me start by saying that I don't understand completion at all, so
> this may be a) simple or b) impossible or c) have to be done a
> completely different way.
>
> I have a function called 'jump' which I will include below.
>
> The function takes one (1) argument from a specific list of arguments.
> I have defined these arguments as part of the function, although they
> don't have to be defined that way if there's another way of doing it
> that would work better for completion.
[snip]
> As you can see, these files are obscurely named in an obscure folder
> on my Mac, but I _could_ create links to all of them with the
> preferred names, such as
>
> ~/jump/m1
> ~/jump/mini
> ~/jump/mba
> ~/jump/tyrion
> ~/jump/imac
>
> and then all the function would need to do is open the file that
> matches the argument.
>
> I'd like to be able to type "jump [tab]" and have all of the options
> appear (m1, mini, mba, tyrion, imac) or type "jump m[tab]" and have
> just the ones that start with 'm' appear, etc. I think that's all
> fairly standard completion stuff.
>
> Any help would be appreciated.

For a very quick and dirty solution, dump this in your .zshrc
compdef -e '() { local expl; _wanted jump-targets expl "jump target"
compadd m1 mini mba tyrion imac }' jump

The next step up is to create a file called _jump, put it in your
$fpath and ensure fpath is set correctly before calling compinit, and
then the contents of that file would be:
#compdef jump
local expl
_wanted jump-targets expl "jump target" compadd m1 mini mba tyrion imac

And of course from here you can get more fancy with generating the
list of targets.

For any more complex handling, ie if jump were to support some
--options and multiple arguments, you would probably want to start
using _arguments instead.

-- 
Mikael Magnusson



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