如何使zsh函数的自动建议使用语法突出显示 [英] How do I make the autosuggestions of a zsh function use syntax highlighting

查看:75
本文介绍了如何使zsh函数的自动建议使用语法突出显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用zsh并编写了一个函数来替换cd函数.在一些帮助下,我得到了它的工作,就像我想要的那样(大多数情况下).这是对我的另一个问题的跟进.该功能几乎可以按我希望的方式工作,但是语法突出显示和自动完成功能仍然存在一些问题.

I use zsh and wrote a function to replace cd function. With some help I got it to work like I want it to (mostly). This is a followup to one of my other question. The function almost works like I want it to, but I still have some problems with syntax highlighting and autocompletion.

对于示例,假设您的目录如下:

For the examples, lets say your directories look like this:

/
    a/
        b/
        c/
            d/
        some_dir/

我还假设已获取以下代码:

I am also assuming the following code has been sourced:

cl () {
    local first=$( echo $1 | cut -d/ -f1 )
    if [ -d $first ]; then
        pushd $1 >/dev/null # If the first argument is an existing normal directory, move there
    else
        pushd ${PWD%/$first/*}/$1 >/dev/null # Otherwise, move to a parent directory or a child of that parent directory
    fi
}
_cl() {
    _cd
    pth=${words[2]}
    opts=""
    new=${pth##*/}
    local expl
    # Generate the visual formatting and store it in `$expl`
    _description -V ancestor-directories expl 'ancestor directories'
    [[ "$pth" != *"/"*"/"* ]] && middle="" || middle="${${pth%/*}#*/}/"
    if [[ "$pth" != *"/"* ]]; then
        # If this is the start of the path
        # In this case we should also show the parent directories
        local ancestor=$PWD:h
        while (( $#ancestor > 1 )); do
            # -f: Treat this as a file (incl. dirs), so you get proper highlighting.
            # -Q: Don't quote (escape) any of the characters.
            # -W: Specify the parent of the dir we're adding.
            # ${ancestor:h}: The parent ("head") of $ancestor.
            # ${ancestor:t}: The short name ("tail") of $ancestor.
            compadd "$expl[@]" -fQ -W "${ancestor:h}/" - "${ancestor:t}"
            # Move on to the next parent.
            ancestor=$ancestor:h
        done
    else
        # $first is the first part of the path the user typed in.
        # it it is part of the current direoctory, we know the user is trying to go back to a directory
        first=${pth%%/*}
        # $middle is the rest of the provided path
        if [ ! -d $first ]; then
            # path starts with parent directory
            dir=${PWD%/$first/*}/$first
            first=$first/
            # List all sub directories of the $dir/$middle directory
            if [ -d "$dir/$middle" ]; then
                for d in $(ls -a $dir/$middle); do
                    if [ -d $dir/$middle/$d ] && [[ "$d" != "." ]] && [[ "$d" != ".." ]]; then
                        compadd "$expl[@]" -fQ -W $dir/ - $first$middle$d
                    fi
                done
            fi
        fi
    fi
}
compdef _cl cl

问题:我使用语法高亮显示,但是我键入的路径只是白色(当转到父目录时.正常的cd函数是彩色的).

The problem: I use syntax-highlighting, but the path i type is just white (when going to a parent directory. the normal cd functions are colored).

示例:

$ cd /a
$ cl c # 'c' is colored
$ pwd
/a/c
$ cl a/b # 'a/b' is not colored
$ cl a/[tab] # 'a/b', 'a/c' and 'a/some_dir' are not colored

如何使这些路径变色?

推荐答案

对于 zsh-syntax-highlighting 插件.它只会检查您输入的内容是否是

That's not possible out of the box with the zsh-syntax-highlighting plugin. It checks only whether what you've typed is

  • A)有效的绝对路径或
  • B)相对于当前工作目录的有效路径.

这并非特定于您的命令.当指定其他命令的路径参数时,突出显示也将失败,这些命令原本是有效的,但不是绝对路径或相对于当前工作目录的有效路径.

This is not specific to your command. Highlighting also fails when specifying path arguments to other commands that are otherwise valid, but are not absolute paths or valid paths relative to the present working dir.

例如:

% cd /a/b
% cd b c  # perfectly valid args, but will not get highlighted as valid paths
% pwd
/a/c

这篇关于如何使zsh函数的自动建议使用语法突出显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆