zsh自动完成在Mac上不建议使用别名命令。

huangapple go评论53阅读模式
英文:

zsh auto completion does not suggest at aliased command on Mac

问题

Sure, here's the translation of your code:

我正在使用别名,像这样
```bash
_m_cdvi() {
    if [ "$#" -eq 0 ] ; then
        builtin cd ~
        pwd
    elif [ "$1" = "-" ] ; then
        builtin cd -
        pwd
    elif [ -d "$1" ] ; then
        builtin cd $1
    elif [ -e "$1" ] ; then
        _m_vicd $@
    else
        echo "不存在"
    fi
}

_m_vicd() {
    if [ "$#" -eq 0 ] ; then
        vim
    elif [ "$1" = "-" ] ; then
        echo " -"
    elif [ -d "$1" ] ; then
        echo "$1 是目录"
        builtin cd $1
    else
        vim $@
    fi
}
alias cd=_m_cdvi
alias vi=_m_vicd

直到上周,它支持了我期望的自动补全。

但是星期一,我更新了我的 macOS,它建议像这样...

giuk@giug-ui-MacBookPro txt % ls
b   bb  bba
giuk@giug-ui-MacBookPro txt % vi - # 当我第一次按tab键时,会显示 -
-1  -- 从组中仅删除连续重复的第一个 # 第二次按tab键,会出现这个...
-2  -- 保留所有重复项
-A  -- 使用扩展匹配填充数组,而不是将它们添加到数组中
-D  -- 从与非匹配候选项对应的数组中删除元素
-F  -- 指定忽略模式的数组
-I  -- 指定被忽略的后缀
-J  -- 指定匹配组
-M  -- 指定匹配规范
-O  -- 使用匹配项填充数组,而不是将它们添加到数组中
-P  -- 指定前缀
-Q  -- 禁用可能补全的引号
-R  -- 指定后缀自动移除的函数
-S  -- 指定后缀
-X  -- 指定解释
-i  -- 指定被忽略的前缀
-n  -- 在补全列表中隐藏匹配项
-o  -- 指定根据匹配字符串而不是显示字符串的顺序匹配
-p  -- 指定隐藏的前缀
-q  -- 使后缀自动移除
-r  -- 指定后缀自动移除的字符类
-s  -- 指定隐藏的后缀
-x  -- 指定无条件解释

如果我尝试使用 b 进行自动补全,什么也不会发生。

我经常在两个命令的最后一步改变主意。

所以我不想放弃这个别名。如何同时使用别名和自动补全?

我在 macOS 中使用 iTerm2

版本信息

macOS:13.3.1(22E261)
iTerm2:版本 3.4.18
giuk@giug-ui-MacBookPro ~ % zsh --version
zsh 5.9 (x86_64-apple-darwin22.0)
giuk@giug-ui-MacBookPro ~ % echo $BASH_VERSION
  # 无输出
giuk@giug-ui-MacBookPro ~ % bash --version
GNU bash,版本 3.2.57(1)-release (arm64-apple-darwin22)
Copyright (C) 2007 Free Software Foundation, Inc.

I hope this helps! If you have any more questions or need further assistance, feel free to ask.

<details>
<summary>英文:</summary>

I&#39;m using alias like
```bash
_m_cdvi() {
    if [ &quot;$#&quot; -eq 0 ] ; then
        builtin cd ~
        pwd
    elif [ &quot;$1&quot; = &quot;-&quot; ] ; then
        builtin cd -
        pwd
    elif [ -d &quot;$1&quot; ] ; then
        builtin cd $1
    elif [ -e &quot;$1&quot; ] ; then
        _m_vicd $@
    else
        echo &quot;not exist&quot;
    fi
}

_m_vicd() {
    if [ &quot;$#&quot; -eq 0 ] ; then
        vim
    elif [ &quot;$1&quot; = &quot;-&quot; ] ; then
        echo &quot; -&quot;
    elif [ -d &quot;$1&quot; ] ; then
        echo &quot;$1 is directory&quot;
        builtin cd $1
    else
        vim $@
    fi
}
alias cd=_m_cdvi
alias vi=_m_vicd

Until last week, it supported autocompletion as I intended.

But monday, I updated my macOS, and it suggest like...

giuk@giug-ui-MacBookPro txt % ls
b   bb  bba
giuk@giug-ui-MacBookPro txt % vi - # when I first press tab, - is written.
-1  -- remove only consecutive duplicates from group # second press tab, this comes..
-2  -- preserve all duplicates
-A  -- populate array with expanded matches instead of adding them
-D  -- delete elements from array corresponding to non-matching candidates
-F  -- specify array of ignore patterns
-I  -- specify ignored suffix
-J  -- specify match group
-M  -- specify matching specifications
-O  -- populate array with matches instead of adding them
-P  -- specify prefix
-Q  -- disable quoting of possible completions
-R  -- specify function for suffix autoremoval
-S  -- specify suffix
-X  -- specify explanation
-i  -- specify ignored prefix
-n  -- hide matches in completion listing
-o  -- specify order for matches by match string not by display string
-p  -- specify hidden prefix
-q  -- make suffix autoremovable
-r  -- specify character class for suffix autoremoval
-s  -- specify hidden suffix
-x  -- specify unconditional explanation

if I tried auto completion with b, nothing happen.

I often change my mind at the last step of the two commands.

So I don't want to give up the alias. How can I get both alias and autocompletion

I'm using iTerm2 in macOS

version info

macOS : 13.3.1(22E261)
iTerm2 : Build 3.4.18
giuk@giug-ui-MacBookPro ~ % zsh --version
zsh 5.9 (x86_64-apple-darwin22.0)
giuk@giug-ui-MacBookPro ~ % echo $BASH_VERSION
  # nothing
giuk@giug-ui-MacBookPro ~ % bash --version
GNU bash, version 3.2.57(1)-release (arm64-apple-darwin22)
Copyright (C) 2007 Free Software Foundation, Inc.

答案1

得分: 2

这是关于 zsh 5.9 中的一个更改引发的副作用,旨在改进完成助手函数的自动补全。它使以下划线开头且没有更具体完成函数的所有命令都像compadd一样完成,而不是默认方式。

解决此问题的简单方法是从函数名称中删除前导下划线(zsh 的约定是具有前导下划线的函数用于生成完成相关内容)。

或者,您可以让它们始终像vim一样完成:

compdef _m_cdvi=vim
compdef _m_vicd=vim
英文:

This is a side-effect from a change first included in zsh 5.9 to improve the autocompletion for completion helper functions. It makes all commands that start with an underscore and don't have a more specific completion function complete like compadd, instead of the default.

The simple fix for this is to remove the leading underscores from your function names (the zsh convention is that functions with a leading underscore are used for/related to generating completions).

Alternatively, you could make them always complete like vim:

compdef _m_cdvi=vim
compdef _m_vicd=vim

huangapple
  • 本文由 发表于 2023年4月19日 16:26:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76052279.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定