Search code examples
zshcompletion

ZSH completion removes the argument with compadd -U


I'm trying to add a completion to a custom vs function which basically open the first filename matching the argument.

OPTIONAL (If you want more information about this function you can find my medium post here => https://medium.com/@victor.boissiere/how-to-quickly-open-files-with-your-editor-1a51b3fe21bf)

In my current folder I have the following:

  • ./example.sh
  • ./custom/directory.sh
  • ./custom/example.sh

Behavior

vs direct<TAB> => completes to custom/directory.sh SUCCESS

vs example<TAB> => vs

Why does it removes the argument and does not let me choose between example.sh and custom/example.sh ?

Code

_vs() {
  local curcontext="$curcontext" state line expl

  _arguments -C \
    '*:: :->open_files'

  case "$state" in
    open_files)
      local file=${words[CURRENT]}
      compadd -U - `find . -type f -ipath "*$file*" | sed "s|^\./||"`
      ;;
  esac
  return 0
}

compdef _vs vs

Solution

  • I just found the solution thanks to this post => https://superuser.com/questions/1264778/changing-to-a-directory-found-somewhere-in-the-tree-hierarchy/1278919#1278919

    I just needed to add compstate[insert]=menu # no expand

    Solution

    _vs() {
      local curcontext="$curcontext" state line expl
    
      _arguments -C \
        '*:: :->open_files'
    
      case "$state" in
        open_files)
          local file=${words[CURRENT]}
          compadd -U - `find . -type f -ipath "*$file*" | sed "s|^\./||"`
          compstate[insert]=menu # add this
          ;;
      esac
      return 0
    }
    
    compdef _vs vs