17

In zsh in vim mode, I write something

foo foo bar bar

Then I hit Esc to go in normal mode and type :

foo foo bar bar
execute: _

Then, I assume I can do that, I want to search and replace bar with baz

foo foo bar bar
execute: s/bar/baz/g_

Then I try to hit Enter, but that doesn't work. I can only go back from the execute using Ctrl+c.

How can I execute the command?

I tried this with my normal .zshrc and without any .zshrc at all.

techraf
  • 5,831
  • 10
  • 33
  • 51
pfnuesel
  • 5,702
  • 8
  • 35
  • 60
  • The accepted answer doesn't seem to answer your question of how you do a substitute command in zsh vim mode. @pfneusel, did you ever find a way to do exactly what you have above? – jspencer Jan 13 '21 at 23:31
  • @jspencer No, I would type `v` and enter `vim`, if I want to use the substitute command. – pfnuesel Jan 14 '21 at 12:02

1 Answers1

13

The vicmd mode, despite the name, is for Vi's normal-mode commands. The prompt started by : isn't for Vi's ex-mode commands, but for running ZLE (Zsh's line editor) commands:

$ echo foo
execute: e_
edit-command-line          emacs-forward-word         end-of-history             end-of-line-hist           exchange-point-and-mark    execute-named-cmd          expand-history             expand-or-complete-prefix
emacs-backward-word        end-of-buffer-or-history   end-of-line                end-of-list                execute-last-named-cmd     expand-cmd-path            expand-or-complete         expand-word

Pressing Enter will work when a unique prefix has been entered (i.e, say you type ed, which matches edit-command-line, then Enter will work). You can press Tab to see matching commands.

muru
  • 69,900
  • 13
  • 192
  • 292
  • Thanks. A reason for using `bash` then instead of `zsh`. – pfnuesel Apr 05 '16 at 12:01
  • @pfnuesel it is? Does Bash support using Vi's ex commands? – muru Apr 05 '16 at 12:02
  • 2
    Typing `v`, you can enter `vim` and do whatever you can do in `vim`. – pfnuesel Apr 05 '16 at 12:19
  • 3
    @pfnuesel Oh, that. Zsh has that. It's not enabled by default. See http://unix.stackexchange.com/a/6622/70524 – muru Apr 05 '16 at 12:21
  • 1
    @muru Your answer is a bit misleading. What you’re referring to as the “vicmd mode” is in fact the `command` keymap — that’s the “ex-like” one that gives you the `execute:` prompt for entering ZLE commands. The `vicmd` keymap *is* for working vi(m)-style command mode. When using zsh in vi mode, you’ll likely be swapping back and forth between `vicmd` and `viins` in much the same way that you move back an forth between command and insert mode in vi(m). – wjv Apr 11 '16 at 07:17
  • @wjv I'm used to calling it normal mode. I hear command mode and I think command-line mode. I rephrased that part. – muru Apr 11 '16 at 12:59