I'm uzing zsh and, because I'm an avid Vim user, I just switched to using Vim-style editing on the command line. (That means if I type ls foo/bar
and decide I want to cd
instead, I can hit Esc ^
to hop back to the beginning and cw cd
to change ls
to cd
.
That's all dandy, but the problem is that I can no longer use Ctrl+R
to do recursive history search. Is there another way to do it, or can I somehow override the Vim keybinding to get it back?
Best Answer
Of course you can no longer use Ctrl+R. If you consult the Z Shell manual you'll see that there's only a key binding for the
history-incremental-search-backward
widget in theemacs
keymap. There are no key bindings for it in the vi keymaps.But as you'll also find from reading the manual (It's chapter 18.), adding a key binding is a fairly simple exercise in the use of the
bindkey
command:You don't even have to use the
zle
command to map the widget onto a shell function, since this is a standard widget.If you consult the answer to this same question that is on the Z Shell wiki, you'll see the commands for specifically adding this to the vi "command" and "insert mode" keymaps:
Also note that, as garyjohn points out, in the vi "command" keymap, the / character is bound to the
vi-history-search-backward
widget. The difference between this widget and thehistory-incremental-search-backward
widget is the widget behaviour that applies once one is in history search mode. Here are a couple of the differences that you will notice:history-incremental-search-backward
toggles between themain
andvicmd
keymaps when you invoke thevi-cmd-mode
widget whilst still staying in search mode. i.e. fromemacs
mode presssing the Esc key or Ctrl+XCtrl+V keys toggles the search mode between theemacs
andvicmd
keymaps. (Invokinghistory-incremental-search-backward
from thevicmd
keymap is thus troublesome, unless you bind something tovi-cmd-mode
in thevicmd
keymap as well.)vi-history-search-backward
treats thevi-cmd-mode
widget asaccept-line
and will end the search, re-entering the command mode that you entered the search from. i.e. (with the default bindings) / enters search mode from command mode and Esc goes back to command mode.history-incremental-search-backward
, both thehistory-incremental-search-backward
and thevi-rev-repeat-search
widgets are recognized. i.e. (presuming that you have altered the bindings as above) both Ctrl+R and N will search for a preceding matching line.vi-history-search-backward
, only thevi-rev-repeat-search
widget is recognized. i.e. (presuming that you have altered the bindings as above) Ctrl+R will cause a beep and be ignored.