Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I use vim for essentially all my editing needs, so I decided to once again try vi-mode for my shell (currently ZSH w/ oh-my-zsh on OS X), but I find myself trying (and failing) to use Ctrl-R constantly. What's the equivalent key-binding? And for future reference, how would I figure this out myself? I'm pretty sure I could use bind -P in bash.

share|improve this question
2  
According to 'man zshzle', history-incremental-search-backward is not bound in Vi modes (vicmd, viins) by default. – paul Jul 27 '12 at 13:45
up vote 39 down vote accepted

You can run bindkey with no arguments to get a list of existing bindings, eg:

# Enter vi mode
chopper:~> bindkey -v

# Search for history key bindings
chopper:~> bindkey | fgrep history
"^[OA" up-line-or-history
"^[OB" down-line-or-history
"^[[A" up-line-or-history
"^[[B" down-line-or-history

In emacs mode, the binding you want is history-incremental-search-backward, but that is not bound by default in vi mode. To bind Ctrl-R yourself, you can run this command, or add it to your ~/.zshrc:

bindkey "^R" history-incremental-search-backward

The zshzle manpage (man zshzle) has more information on zsh's line editor, bindkey, and emacs/vi modes.

share|improve this answer
3  
Thanks, particularly for zshzle. That is quite informative and useful. – Hank Gay Jul 27 '12 at 15:27
    
I love you. Seriously. After upgrading to OSX El Capitan reverse search was gone and I missed it more than everything else in my daily workflow. – Christian Oct 17 '15 at 18:09

This is an ancient question, but the only (and accepted) answer basically tells one how to transplant the “emacs-like” history-incremental-search-backward to vi mode. Whilst this is perfectly doable and for many the right solution, it’s a little strange that no one has mentioned the “vi way” of searching history.

vi mode in zsh supports searching history using the standard vi/vim keys: / and ?, both available in command mode. (Hit <Esc> to switch from insert to command mode, just like in vi or vim.)

Their sense is reversed, though: Since you usually want to search your shell’s history in reverse, / does a reverse search whereas ? does a forward search.

Once the first hit is displayed, you can (just like in vi/vim) use n to continue finding more hits in the same direction, or N to reverse the direction of the search.

The relevant default keybindings in the vicmd keymap are:

"/" vi-history-search-backward
"?" vi-history-search-forward
"n" vi-repeat-search
"N" vi-rev-repeat-search
share|improve this answer
    
Great answer. Joined just to upvote. – Travis Jul 12 at 19:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.