Zsh function: forward completion to subfunction
Asked Answered
M

1

7

Many times I end up writting wrapper function around existing ones, for instance:

function gl {
    some_computed_stuff=...
    git --no-pager log --reverse $some_computed_stuff "$@"
}
function m {
    make "$@" && notify-send success || notify-send failed
}

I know that aliases keep autocompletion but sometimes functions are required and in that case autocompletion is lost. For instance here I would like to keep git log completion for my function gl or make completion for m.

I tried to add compctl -K _git gl but no suggestions are made. It won't work anyway since I must somehow find how to provide log argument to _git autocompletion script as well, so my question is:

Is there a way to make ZSH (but also bash) understand that typing gl is the exact equivalent of git log? Something like (for ZSH only):

compctl 'git log' gl
compctl 'make' m 
Massif answered 1/12, 2016 at 11:22 Comment(0)
C
8

For zsh, you can create a new completion with compdef function.

In its basic form it associates a completion function with a word. Provided that zsh comes with lots of completions already built-in, one can just reuse those. For example, for m function from the question:

$ compdef _make m

As per documentation, you can also define a completion for a specific service, if the one is defined in the completion function. Again, as zsh comes with _git completion and it already defines git-log service, a gl function from the question may be autocompleted with:

$ compdef _git gl=git-log

On Linux, you can see existing completion implementations in /usr/share/zsh/functions/Completion/Unix/. You can read the completion implementations to see what services they define.

Coffle answered 9/12, 2016 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.