Search code examples
macosautocompletezsh

zsh auto completion does not suggest at aliased command on Mac


I'm using alias like

_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 "not exist"
    fi
}

_m_vicd() {
    if [ "$#" -eq 0 ] ; then
        vim
    elif [ "$1" = "-" ] ; then
        echo " -"
    elif [ -d "$1" ] ; then
        echo "$1 is directory"
        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.

Solution

  • 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