87

In bash, using vi mode, if I hit Esc,v, my current command line is opened in the editor specified by $EDITOR and I am able to edit it in full screen before 'saving' the command to be returned to the shell and executed.

How can I achieve similar behaviour in zsh? Hitting v in command mode results in a bell an has no apparent effect, despite the EDITOR environment variable being set.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Murali Suriar
  • 2,734
  • 4
  • 21
  • 24

6 Answers6

62

In case you prefer Emacs keybindings:

autoload -z edit-command-line
zle -N edit-command-line
bindkey "^X^E" edit-command-line
Daniel Serodio
  • 1,123
  • 1
  • 9
  • 14
  • 20
    The `^x^e` binding seems to be a default with [current .oh-my-zsh](https://github.com/robbyrussell/oh-my-zsh/blob/9650861e56a3404313adc35cbcb1f32a7015b99d/lib/key-bindings.zsh#L65), which is extremely nice. – quodlibetor Aug 08 '14 at 15:44
  • ^x^e is exactly what I was looking for. – jmreicha Nov 17 '14 at 16:30
  • 1
    Similar setup for Prezto as long as the `editor` module is loaded. – AL the X Jun 09 '15 at 12:08
61

See edit-command-line in zshcontrib.

bindkey -M vicmd v edit-command-line
ephemient
  • 15,640
  • 5
  • 49
  • 39
  • 23
    (Just to clarify): To enabled the `edit-command-line` “widget” you will need something like `autoload edit-command-line; zle -N edit-command-line` in one of your *zsh* init files (see the [ZLE Functions section](http://zsh.sourceforge.net/Doc/Release/User-Contributions.html#SEC283) of the [`zshcontrib` documentation](http://zsh.sourceforge.net/Doc/Release/User-Contributions.html)). – Chris Johnsen Jan 29 '11 at 03:14
  • 2
    How do you activate this once it's in place? The bindkey in the emacs-style answer makes sense to me. But I can't find reference on what `bindkey -M vicmd v` sets up. – Mat Schaffer May 25 '12 at 18:50
  • 3
    @MatSchaffer Same as Bash: if you are in vi mode, then hit `` to enter command mode from insert mode and and then `v` to visual mode (`Meta-v` is the same), or just `v` if you're already in command mode. If you're using Emacs mode, the typical binding is `C-x C-e`. – ephemient May 25 '12 at 20:03
  • 6
    Remapping the v key means you can't use visual mode any more, so I prefer remapping to the spacebar (which doesn't do anything useful in normal mode) with `bindkey -M vicmd ' ' edit-command-line` – gib Sep 04 '18 at 23:24
46

You can use fc to edit the last command in history. It's not the same as editing the same command, but a quick hit on the Enter key makes your current command the last command in history.

Christoph Wurm
  • 5,678
  • 5
  • 25
  • 28
  • 3
    This is great when you are ssh'ed into some other machine which does not have `edit-command-line` setup as one may have in their personal environment. – anishpatel Mar 10 '20 at 18:28
  • what does fc stands for? f-command...? – fabrizioM Jan 07 '23 at 03:37
  • This is almost certainly what you want. Just about any time your command is complex enough to want to edit it, you've already tried a few different variations of it so it's in your history. To add on to this, you can use `fc $ENTRY` to edit that command in your `history`. `zshbuiltins(1)` has more info. And, then, when it works, you can use `fc` again to save it to a file to run later as a script! – anahata Mar 18 '23 at 23:14
30

This is the complete configuration that I added to my ~/.zshrc to get the same behavior from bash:

export VISUAL=vim
autoload edit-command-line; zle -N edit-command-line
bindkey -M vicmd v edit-command-line
Anthon
  • 78,313
  • 42
  • 165
  • 222
Matt Hughes
  • 401
  • 4
  • 2
  • 1
    Per the earlier comments, this is configured by default if you're using [Oh my ZSH](https://github.com/robbyrussell/oh-my-zsh) or [Prezto](https://github.com/sorin-ionescu/prezto). The latter requires the `editor` module to be loaded and `vi` or `vim` selected for command mode. – AL the X Jun 09 '15 at 12:10
  • I'd vote to make this the accepted answer, it solved my question with all configuration described. Thanks. – Laurent Jul 17 '16 at 20:14
  • 1
    For me, `v` should surrounded by quote: `'v'`. – roachsinai Aug 21 '20 at 15:06
  • but I lost my cursor after existing vim – apollo Nov 09 '20 at 22:15
  • 1
    @roachsinai, if `v` were a special character in the syntax of `zsh`, you'd need `bindkey -M 'v'icmd 'v' edit-command-line` and `export VISUAL='v'im` but of course `v` is not special in the syntax of zsh or any shell for obvious reasons, so the quotes are superflous. They'd only be needed if you had something like `alias -g v=gotcha` in your `~/.zshrc` (and no `alias -g "'v'=ahah"`) – Stéphane Chazelas Sep 20 '22 at 14:01
  • this works as expected with `zsh 5.9 (arm-apple-darwin22.1.0)`. hooray for proper full screen command line editing! – Lex Scarisbrick Jul 06 '23 at 00:46
1

In case you're like me wanting to emulate oh-my-zsh behaviour that is to open the current line in vim when you press 'vv' in normal mode, use the following :

autoload -Uz edit-command-line
zle -N edit-command-line
bindkey -M vicmd 'vv' edit-command-line
cassepipe
  • 174
  • 1
  • 15
0

I think @quodlibetor's comment is worth an answer on its own:

If you are using .oh-my-zsh, the default key compination is:

Ctrl+x followed by Ctrl+e

Brimstedt
  • 125
  • 5