Search code examples
zshoh-my-zshzshrczsh-completionzsh-zle

Is there a way to check if the tab completion menu is open in ZSH


Is there a way to check if the tab completion menu is open in ZSH?

I am asking this question because I want to bind the space key to accept-search so that I can accept completion with the space key, but I want to do it in a way that doesn't interfere with the default behaviour of the space key (inserting the space character). Also, I want this because I don’t like that when I close the completion menu with space it inserts an space.

Someone in reddit helped me with this snippet, but I need a way to check if the autocompletion menu is open or not.

function space-or-accept-suggestion() {
  if <INSERT WAY TO CHECK IF AUTOCOMPLETION MENU IS OPEN>; then
    zle accept-search
  else
    zle self-insert
  fi
}

zle -N space-or-accept-suggestion
bindkey -M emacs ' ' space-or-accept-suggestion

Solution

  • You don't need to check that. Just use this:

    zmodload zsh/complist
    bindkey -M menuselect ' ' accept-search
    

    More info can be found here: http://zsh.sourceforge.net/Doc/Release/Zsh-Modules.html#Menu-selection