Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zsh function: forward completion to subfunction

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 
like image 287
bagage Avatar asked Dec 01 '16 11:12

bagage


People also ask

Can zsh use bash completion?

Zsh can handle bash completions functions. The latest development version of zsh has a function bashcompinit, that when run will allow zsh to read bash completion specifications and functions. This is documented in the zshcompsys man page. To use it all you need to do is run bashcompinit at any time after compinit.

Where do I put zsh completion?

Since zsh 5.0. 7, the default function load path ( $fpath ) includes /usr/local/share/zsh/site-functions . So install your completion functions there.

What is zsh completion?

`Completion' is where you hit a particular command key (TAB is the standard one) and the shell tries to guess the word you are typing and finish it for you --- a godsend for long file names, in particular, but in zsh there are many, many more possibilities than that.

What does Compinit do in zsh?

Autoloaded files Furthermore, if the directory in question ends in the path segment Core, or has a subdirectory named Core, compinit will add all subdirectories of the directory where Core is to the path: this allows the functions to be in the same format as in the zsh source distribution.


1 Answers

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.

like image 166
Timur Avatar answered Nov 11 '22 11:11

Timur