I recently switched the history search to use history-incremental-pattern-search-backward since it allows for patterns in the search. Here is how it is setup in my .zshrc

bindkey -M vicmd '/' history-incremental-pattern-search-backward

This works great, but I can't figure out how to go to the next match from the search menu.

% cat foobarbaz.txt
bck-i-search: f*baz

Is there a key bound to do this?


Finally found the right incantation. You need to map the pattern search in insert mode. This was the piece I was missing:

# Search backwards and forwards with a pattern
bindkey -M vicmd '/' history-incremental-pattern-search-backward
bindkey -M vicmd '?' history-incremental-pattern-search-forward

# set up for insert mode too
bindkey -M viins '^R' history-incremental-pattern-search-backward
bindkey -M viins '^F' history-incremental-pattern-search-forward

I would have used ^B for backwards search in insert mode, but I have that mapped to something else. So now ^R and ^F cycle through the matches.

  • what does the -M command do? In my case even without the -M keyword this works great.. – alpha_989 Apr 6 '18 at 16:24
  • The -M keymap tells bindkey which keymap to apply the binding to. I've set it up for both insert mode and command mode. – claytron Apr 9 '18 at 18:12
  • Thanks for the clarification. In my case I just have bindkey '^R' history-incremental-pattern-search-backward, and it works. I also use the default / command in vi normal model in zsh. In that I can search the command history, and once it finds the command, it will just provide that at the command line. Then I can modify the last command if necessary. – alpha_989 Apr 15 '18 at 16:04
  • If I use '^R' history-incremental-pattern-search-backward, on the other hand, it will just land me to the last command that matches the search string. However, I haven't found a way to actually be able to edit the command. After finding a suitable command matching the search string through incremantal patter search, are there any specific key combinations, that I can press to be able to edit the command? – alpha_989 Apr 15 '18 at 16:06

According to the zshzle manual page for zsh 4.3.10, no key is bound by default. You need to add bindings to the isearch map, and only a few widgets are available. For instance, you can do:

bindkey -M isearch '^R' history-incremental-search-backward
bindkey -M isearch '^S' history-incremental-search-forward
  • bindkey -l does not show an isearch mapping – claytron Oct 10 '10 at 16:25
  • @claytron: must be a version issue: isearch is new in 4.3.10, while history-incremental-pattern-search-backward appeared in 4.3.7 (if I read the changelog correctly). – Gilles Oct 10 '10 at 16:54
  • I'm using 4.3.10, not sure why it isn't there. Using the insert mode bindings worked for me though. – claytron Oct 10 '10 at 18:20

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.