【问题标题】:Using bash function + grc like an alias breaks Zsh completion像别名一样使用 bash 函数 + grc 会破坏 Zsh 完成
【发布时间】:2016-10-12 02:05:29
【问题描述】:

我正在使用 grc / grcat 使用此功能为我的终端输出着色:

function docker() {
  case $* in
    ps* ) shift 1; command docker ps "$@" | grcat $HOME/.grc/conf.dockerps ;;
    images* ) shift 1; command docker images "$@" | grcat $HOME/.grc/conf.dockerimages ;;
    info* ) shift 1; command docker info "$@" | grcat $HOME/.grc/conf.dockerinfo ;;
    * ) command docker "$@" ;;
  esac
}

但这破坏了我在 .zshrc 中使用 oh-my-zsh 设置的 zsh 完成和插件:

plugins=(git colored-man-pages colorize vagrant brew ruby docker)
plugins+=(gradle rvm rails bundler gem osx pod)
plugins+=(bower cp go node npm)
plugins+=(zsh-syntax-highlighting encode64)
plugins+=(zsh-completions mvn)

fpath=(~/.zsh/completion $fpath)

autoload -Uz compinit && compinit -i

例如,当我尝试使用 docker stop 时,ANSI 转义序列将完成:$'\033'\[0m$'\033'\[1m$'\033'\[33mhigh_wright$'\033'\[0m

有没有办法避免这种情况,只为完成过滤那些转义序列?

编辑

我在 .oh-my-zsh 目录中为 docker 插件找到了这个文件:https://github.com/robbyrussell/oh-my-zsh/blob/master/plugins/docker/_docker

我知道我需要更改此功能,但我不知道如何。

__docker_containers() {
    declare -a cont_cmd
    cont_cmd=($(docker ps | awk 'NR>1{print $NF":[CON("$1")"$2"("$3")]"}'))
    if [[  'X$cont_cmd' != 'X' ]]
        _describe 'containers' cont_cmd
}

【问题讨论】:

    标签: bash zsh oh-my-zsh ansi-escape zsh-completion


    【解决方案1】:

    我可以通过更改安装在.oh-my-zsh/plugins/docker/_docker 中的 oh-my-zsh 中的 docker 插件来解决此问题。为了解决这个问题,我在所有辅助函数之前都使用了command,如下所示:

    __docker_containers() {
        declare -a cont_cmd
        cont_cmd=($(command docker ps | awk 'NR>1{print $NF":[CON("$1")"$2"("$3")]"}'))
        if [[  'X$cont_cmd' != 'X' ]]
            _describe 'containers' cont_cmd
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-04-09
      • 2011-01-11
      • 2012-04-18
      • 2011-02-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多