Editing shell command in vim on the fly from the terminal

2.9k Views Asked by At

I just saw a video where a guy were doing some commands and then he needed to edit the command that he was currently typing in the console.

It looked like he was doing some shortcut and the line that he was typing appeared in a vim buffer, ready for editing and execution.

I am using zsh and my terminals of choice are termite and urxvt. I was wondering how to do this.

  • Is it necessary to use a plugin or can it be done in a simple way?
  • Is it depending on the terminal used or can I just use the shell (zsh)

Thank you in advance

2

There are 2 best solutions below

2
Gre-san On

I guess what you mean is set -o vi on Bash. It will open Vim to edit the current buffer of the terminal.

$ export EDITOR=vim
$ set -o vi
$ aaa bbb ccc ## then, type [ESC] and [v], it'll go to vim buffer.

Zsh doesn't open Vim if above commands are executed. But it simulates vim on the terminal instead, and I think it is enough for in many cases.

As far as I know, Zsh does not support such the behavior like Bash by default.

I found the similar discussion in reddit and it may help you.

0
Xen_mar On

So there are two ways of doing things that worked for me.

1.) Open the current command in a vim editor

Go to your ./zshrc file and add:

export EDITOR=vim

plugins=(
  # ...all your other plugins
  vi-mode
)

Enter a command press ESC to go into command mode and then press v to open vim.

2.) Use vim commands without leaving the terminal session. For this add set -o vi to your ~/.zshrc config.