I've recently switched to vi-mode in my zsh and there is a feature from emacs-mode that I cannot find how to do in vi-mode. The feature is browsing history by lines starting by something I've already typed. For example if I type vi and press or then I browse through my recent commands starting with vi. Is it even possible with vi-mode?


emacs-mode and vi-mode are only different presets of key bindings. Any widget can be bound in any mode.

The widgets in question here are history-beginning-search-backward (presumably for ) and history-beginning-search-forward (for ).

To bind them to the up and down keys in vicmd mode, you just need to run

bindkey '^[[A' history-beginning-search-backward
bindkey '^[[B' history-beginning-search-forward
  • Note 1: This overwrites the default setting for those keys.
  • Note 2: ^[[A and ^[[B may not be the correct codes for your terminal.

    • either Find the correct ones by pressing Ctrl+v followed by the key in question.
    • or make use of the associative array terminfo to retrieve the correct binding (my thanks to Stéphane Chazelas for his comment on this):

      bindkey "$terminfo[kcuu1]" history-beginning-search-backward
      bindkey "$terminfo[kcud1]" history-beginning-search-backward

      kcuu1 and kcud1 are the capability names for and , respectively. You can get a list of possible capabilies and their names from the terminfo(5) manpage (man 5 terminfo). The associative array terminfo uses the names from the "Cap-name" column as keys.

  • 1
    See also $terminfo[kcuu1] to get the sequence for the Up key from the terminfo database. – Stéphane Chazelas Jun 8 '16 at 16:15

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.