Like a lot of zsh users, I have completion configured so that when I am at a command prompt and I invoke completion, a menu appears that can be navigated with the directional arrow keys. i.e. in my ~/.zshrc

autoload -Uz compinit

zstyle ':completion:*' menu yes select
zstyle ':completion:*:*:cd:*' menu yes select

If I want to delete the directory /var/log/nginx/somewebsite, I will start the command using completion like:

ubuntu% sudo rm -rf /var/l<TAB>
lib/    local/  lock@   log/

I navigate the items with the directional arrow keys, which are inserted into the command line as the cursor moves over them; once on the desired selection, hit ENTER basically to exit the menu widget and return the cursor back to the command line.
The problem is ENTER is used for both accepting the item from the completion menu and also executing the command, and I keep hitting ENTER thinking I am accepting completion when I am actually executing these partially complete commands!

Can I remap zsh's "accept completion suggestion and exit widget" command to something other than ENTER?
e.g. TAB or even Ctrl + y (same as vim's accept completion keymap)

  • Which version are you using? My zsh 5.2 (i686-pc-linux-gnu) works as expected - first enter accepts completion, second accepts/executes command line.
    – jimmij
    Jun 25 '16 at 16:29

I found the documentation for this in

  • man zshmodules
      • menu selection

To bind the key Ctrly to accept a completion suggestion, you can use

bindkey ^Y accept-search

There is a note in the documentation

Note that the following (complist functions) always perform the same task within the menu selection map and cannot be replaced by user defined widgets, nor can the set of functions be extended:

so accordingly, you can't unbind Enter away from accept-search, in fact funnily enough the command

bindkey -r ^M accept-search

only succeeds in unbinding accept-line from a normal command line.


You can bind another key to accept a completion suggestion so that it's only active during menu-completion: bindkey -M menuselect <key> accept-search (assuming you use menu-completion).

Also, if you want Enter to immediately execute the command line, rather than having to press it twice to first accept the completion: bindkey -M menuselect '^M' .accept-line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.