1

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
  • You need to be clearer about what state you want to check for. As far as I can tell, what you describe is the default anyway. "autocompletion" as opposed to completion triggered by the tab key is usually only implemented via some plugin like autosuggestions. If you mean menu selection from the complist module is active, that uses the `menuselect` keymap so you can bind space differently for it. But by default, it'll be accepted and a space inserted. – okapi Nov 01 '20 at 17:37
  • Updated the question. It’s tab triggered completion. Thanks! – David Guevara Nov 02 '20 at 20:55

1 Answers1

0

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

Marlon Richert
  • 5,250
  • 1
  • 18
  • 27
  • That doesn't seem to do anything. For example, I type cd ~/ and press tab, select some option, and then press space and it still inserts the space. I think that maybe I should just adapt myself to the default behavior instead of trying to change it, even if only zsh doesn't want to work like that. – David Guevara Dec 06 '20 at 23:02
  • @DavidGuevara Sorry, I forgot you also need to do `zmodload zsh/complist` before using that `bindkey` command. I added it to my answer. Please try again. – Marlon Richert Dec 07 '20 at 14:16