首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何扩展现有的zsh完成函数?

如何扩展现有的zsh完成函数?
EN

Unix & Linux用户
提问于 2018-10-23 00:02:00
回答 1查看 1.9K关注 0票数 1

我试图为一些自定义的ant参数添加自动完成。然而,它似乎是覆盖现有的哦,我的zsh蚂蚁插件自动完成,我想保持。有没有一种简单的方式,既可以让我的zsh插件和我的自定义蚂蚁自动完成和谐生活?

以下是~/.oh-my-zsh/plugins/ant/ant.plugin.zsh现有的插件

代码语言:javascript
复制
_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
    fi
    compadd -- `cat .ant_targets`
  fi
}

compdef _ant ant

我的汽车在~/my_completions/_ant完成了

代码语言:javascript
复制
#compdef ant

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

这是我的$fpath,我的完成路径在列表的前面,我猜这就是为什么我的脚本优先。

代码语言:javascript
复制
/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
EN

回答 1

Unix & Linux用户

回答已采纳

发布于 2019-02-17 01:33:20

我认为最好的方法是扩展完成功能。下面是如何做到这一点:https://unix.stackexchange.com/a/450133

首先,您需要找到现有函数的名称,为此,可以将_complete_help绑定到CTRL(或任何其他快捷方式),然后键入命令并查找完成函数。下面是一个为git运行它的示例:

代码语言:javascript
复制
% 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)

在这种情况下,完成函数是_git。接下来,您可以在.zshrc中像这样重新定义它:

代码语言:javascript
复制
# Call the function to make sure that it is loaded.
_git 2>/dev/null
# Save the original function.
functions[_git-orig]=$functions[_git]
# Redefine your completion function referencing the original.
_git() {
    _git-orig "$@"
    ...
}

您不需要再次调用compdef,因为它已经绑定到该函数,并且您只是更改了函数定义。

票数 3
EN
页面原文内容由Unix & Linux提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://unix.stackexchange.com/questions/477142

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档