1

When vi mode is enabled for zsh (bindkey -v) is there anyway to search the history as per the regular mode, i.e. by typing a few letters and pressing the up/down arrow keys?

I find this much quicker than pressing escape, then ?, typing a few letters, pressing enter then using n to scroll over the matches.

Also it seems that the vi mode search matches anywhere in the command, I only want to match on the start of the command like regular searching with arrow keys does.

1 Answer 1

2

Note that you're discussing to different search methodologies here:

Searching “the vi way” with / or ? will search entire commands stored in history.

What you want the up and down arrow keys to do is something different: You want them to iterate through items stored in history that match (at their start) what you've already typed at the command line.

Without going into too much detail, the following should be a fairly foolproof way of achieving this:

autoload -Uz history-search-end

zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end

bindkey -M vicmd '^[[A' history-beginning-search-backward-end \
                 '^[OA' history-beginning-search-backward-end \
                 '^[[B' history-beginning-search-forward-end \
                 '^[OB' history-beginning-search-forward-end
bindkey -M viins '^[[A' history-beginning-search-backward-end \
                 '^[OA' history-beginning-search-backward-end \
                 '^[[B' history-beginning-search-forward-end \
                 '^[OB' history-beginning-search-forward-end
2
  • Thank you very much! Could you explain how this works?
    – xeruf
    Oct 10, 2019 at 12:03
  • This answer is simpler and works for me. May 15, 2023 at 6:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .