75

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.

4 Answers 4

54

See edit-command-line in zshcontrib.

bindkey -M vicmd v edit-command-line
4
  • 19
    (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 of the zshcontrib documentation). Jan 29, 2011 at 3:14
  • 1
    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. May 25, 2012 at 18:50
  • 2
    @MatSchaffer Same as Bash: if you are in vi mode, then hit <ESC> 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, 2012 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 4, 2018 at 23:24
52

In case you prefer Emacs keybindings:

autoload -z edit-command-line
zle -N edit-command-line
bindkey "^X^E" edit-command-line
3
  • 15
    The ^x^e binding seems to be a default with current .oh-my-zsh, which is extremely nice. Aug 8, 2014 at 15:44
  • ^x^e is exactly what I was looking for.
    – jmreicha
    Nov 17, 2014 at 16:30
  • 1
    Similar setup for Prezto as long as the editor module is loaded.
    – AL the X
    Jun 9, 2015 at 12:08
39

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.

1
  • 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, 2020 at 18:28
26

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
4
  • Per the earlier comments, this is configured by default if you're using Oh my ZSH or Prezto. The latter requires the editor module to be loaded and vi or vim selected for command mode.
    – AL the X
    Jun 9, 2015 at 12:10
  • I'd vote to make this the accepted answer, it solved my question with all configuration described. Thanks.
    – Laurent
    Jul 17, 2016 at 20:14
  • 1
    For me, v should surrounded by quote: 'v'.
    – roachsinai
    Aug 21, 2020 at 15:06
  • but I lost my cursor after existing vim
    – apollo
    Nov 9, 2020 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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