bash - 如何将此 bash 补全函数转换为 zsh 补全函数?

标签 bash zsh

如何将此 bash 补全函数转换为 zsh 补全函数?

此 bash 函数允许您使用制表符完成当前工作目录之上的目录名称,以便您可以更快、更准确地在文件层次结构中导航。

示例:

$ pwd
/Users/me/Animals/Mammals/Ungulates/Goats/Ibex
$ upto Animals
$ pwd
/Users/me/Animals

代码:

function upto {
    if [[ -z $1 ]]; then
        return
    fi
    local upto=$1
    cd "${PWD/\/$upto\/*//$upto}"
}

_upto()
{
    local cur=${COMP_WORDS[COMP_CWORD]}
    local d=${PWD//\//\ }
    COMPREPLY=( $( compgen -W "$d" -- "$cur" ) )
}
complete -F _upto upto

我希望在 zsh 中有一个类似的,但我并不完全理解它的工作原理,更不用说如何为 zsh 修改它了。

最佳答案

尝试这个完成功能

_upto() {
    local parents;
    parents=(${(s:/:)PWD});
    compadd -V 'Parent Dirs' -- "${(Oa)parents[@]}";
 }

使用 compdef _upto upto 启用它.

通过创建一个深度嵌套文件夹来测试它:f=/tmp/foo/bar/one/two/three;mkdir -p $f;cd $f 。打字 upto <Tab>应该给

three  two    one    bar    foo    tmp

要反转目录(即在根目录而不是 .. 启动提案),请删除 (Oa)来自compadd的标志线。

关于bash - 如何将此 bash 补全函数转换为 zsh 补全函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35374305/

相关文章:

bash - 使 Web 服务器进程保持事件状态的终端应用程序

mongodb - 使用 zsh 在 Mac 上找不到 Mongo 命令?

Bash 的 vim 模式,而不是 vi

linux - 如何使用node在Windows CMD中运行文件

linux - 保留一对中的一个成员

linux - 如何使用 awk 提取引用字段?

bash - `-s --` 标志对 npm 有什么作用?

azure - 如何在 zsh 中为 Azure CLI 启用命令完成?

macos - 安装 prezto 时遇到问题

rvm - zsh:找不到命令:rvm-prompt