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.

0

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

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.