I'm trying to add auto-completion for some custom ant parameters. However, it seems to be overwriting the existing oh my zsh ant plugin auto-completions which I'd like to keep. Is there a simple way to have both the oh my zsh plugin and my custom ant auto-completion live in harmony?

Here's the existing plugin at ~/.oh-my-zsh/plugins/ant/ant.plugin.zsh

_ant_does_target_list_need_generating () {
  [ ! -f .ant_targets ] && return 0;
  [ build.xml -nt .ant_targets ] && return 0;
  return 1;

_ant () {
  if [ -f build.xml ]; then
    if _ant_does_target_list_need_generating; then
        ant -p | awk -F " " 'NR > 5 { print lastTarget }{lastTarget = $1}' > .ant_targets
    compadd -- `cat .ant_targets`

compdef _ant ant

And my auto complete at ~/my_completions/_ant

#compdef ant

_arguments '-Dprop=-[properties file]:filename:->files' '-Dsrc=-[build directory]:directory:_files -/'
case "$state" in
        local -a property_files
        property_files=( *.properties )
        _multi_parts / property_files

And here is my $fpath, the path for my completion is earlier in the list which I guess is why my script gets precedence.

/Users/myusername/my_completions /Users/myusername/.oh-my-zsh/plugins/git /Users/myusername/.oh-my-zsh/functions /Users/myusername/.oh-my-zsh/completions /usr/local/share/zsh/site-functions /usr/share/zsh/site-functions /usr/share/zsh/5.0.8/functions

I think the best way to do it is to extend completion function. Here is how you can do it: https://unix.stackexchange.com/a/450133

First you would need to find the name of your existing function, to do it, you can bind _complete_help to CTRL-h (or any other shortcut), then type your command and look for completion function. Here is an example running it for git:

% bindkey '^h' _complete_help  
% git [press ctrl-h]
tags in context :completion::complete:git::
    argument-1 options  (_arguments _git)
tags in context :completion::complete:git:argument-1:
    aliases main-porcelain-commands user-commands third-party-commands ancillary-manipulator-commands ancillary-interrogator-commands interaction-commands plumbing-manipulator-commands plumbing-interrogator-commands plumbing-sync-commands plumbing-sync-helper-commands plumbing-internal-helper-commands  (_git_commands _git)

In this case the completion function is _git. Next you can redefine it in your .zshrc like this:

# Call the function to make sure that it is loaded.
_git 2>/dev/null
# Save the original function.
# Redefine your completion function referencing the original.
_git() {
    _git-orig "$@"

You wouldn't need to call compdef again, since it is already bound to that function, and you just changed the function definition.

  • nice tip on finding the original function and the proper syntax to override it, will give it a go :) – Ralph Callaway Feb 19 at 19:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.