89

I recently switched to zsh (finally) and am loving it! So far one thing that I am missing is Ctrl+R to do incremental history search.
I have the history set up properly

HISTSIZE=10000 SAVEHIST=10000 HISTFILE=~/.zsh_history 

and I used vi key bindings

bindkey -v 

But Ctrl+R does not work. It removes the line above the current line, which is not the behavior it should have in vim either.

Any suggestions?

2
  • 1
    btw a quick work-around is to use non-incremental search: Ctrl+[ / ... Commented Jan 27, 2012 at 14:16
  • 2
    Ctrl-R is an emacs shortcut. To search backwards in vi, it's ?, though in zsh's vi mode / and ? are reversed as you generally want to search backwards. Commented Dec 15, 2019 at 21:33

3 Answers 3

139

If I recall correctly, you need to explicitly set it, even with bindkey -v. Use something like this:

bindkey -v bindkey '^R' history-incremental-search-backward 
7
  • 5
    perhaps history-incremental-pattern-search-backward is an alternative action to use in that context. Commented Jan 27, 2012 at 14:18
  • 3
    @maxschlepzig sorry what is the difference between the two? Commented Jan 27, 2012 at 15:34
  • 4
    @Ali, with the -pattern- versions you can use search-patterns (which style, i.e. globbing or regex (?) depends on other parts of your zsh-configuration) - like e.g. you can use the search-pattern fo*bar with grep to match 'fobar', 'foobar', 'fooobar' etc. Commented Jan 27, 2012 at 21:03
  • 6
    I was getting an error. Wrapping ^R in double quotes did it. bindkey "^R" history-incremental-pattern-search-backward Commented Sep 8, 2013 at 6:26
  • 5
    bindkey -v must precede history-incremental-search-backward Commented Feb 21, 2019 at 7:22
13

bindkey -e also works and makes zsh behave more like bash. It restores other things that you may have been using like ctrl-A (beginning of line), ctrl-K (delete everything to the right of the cursor).

3
  • 1
    This is a great answer, thanks. It solves a couple of problems at once, in a clean way. Commented Mar 30, 2021 at 10:28
  • 5
    Just adding a comment to explain that this sets so-called "emacs mode" which provides a lot of these settings even to those like me who aren't into emacs. Commented Nov 26, 2021 at 22:34
  • 1
    This is super helpful; esp to those coming from Ubuntu background. Commented May 30, 2024 at 19:27
9

OMZ framework has zsh-history-substring-search plugin pre-packaged. Just enable & use; for example:

plugins=(git zsh-history-substring-search) 

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.