2

I've added following code at the end of my .zshrc:

export VISUAL=vim
autoload edit-command-line; zle -N edit-command-line
bindkey -M vicmd v edit-command-line

When I restart the shell, it works perfectly but all previous key bindings stop working.

I'm unable to search history (CTRL-R) and I'm unable to move in command line (CTRL-A/E) for example.

Maybe I placed these commands in the wrong place?

Any help, very appreciated!

gsscoder
  • 123

1 Answers1

2

When you set VISUAL to a string containing vi, zsh uses the Vi keymaps (vicmd, viins, …) with Vi-style default key bindings. Otherwise zsh uses the emacs keymap with Emacs-style default key bindings.

If you want to use Emacs-style command line editing in the shell but use Vim as your favorite editor, add bindkey -e to your .zshrc.

If you want to use Vi-style command line editing, but with some bindings from the Emacs-style defaults, you'll have to define these bindings yourself. ^R in Emacs mode runs history-incremental-search-backward which isn't bound in Vi mode. The default key bindings for search in Vi command mode are / for vi-history-search-backward and ? for vi-history-search-forward. To move to the beginning or end of the command line, you have 0, ^ and $ as usual.

  • bindkey '^R' history-incremental-search-backward restores CTRL-R (not CTRL-A/E). Adding bindkey -e works but I lost the ability to invoke vi using ESC-V. What am I doing wrong? Thanks for your time. – gsscoder Nov 15 '19 at 17:53
  • @gsscoder What do you want to do? You can't both have all Vi bindings and all Emacs bindings: they're sometimes contradictory. To edit the current command with your editor, see https://unix.stackexchange.com/questions/6620/how-to-edit-command-line-in-full-screen-editor-in-zsh – Gilles 'SO- stop being evil' Nov 15 '19 at 18:05
  • 1
    This made perfectly sense. The problem I was lacking a basic concept... Now I understand. Thank you – gsscoder Nov 16 '19 at 05:08
  • I want to explain that I wanted to use vim as editor and just keep CTRL-A/E to move directly in terminal. I've achieved it with this two commands: bindkey '^A' beginning-of-line and bindkey '^E' end-of-line. In https://gist.github.com/gsscoder/6466d661e66b09131f9c0d0945e386d5 it's possible to see all commands I add to my .zprofile. – gsscoder Nov 16 '19 at 06:46