1

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

mama
  • 2,046
  • 1
  • 7
  • 24
  • You can use `fc` command in bash or zsh if you want to edit the **last entered** command in the editor but sadly it won't open currently entered command. – isAif Aug 11 '20 at 09:16

2 Answers2

2

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.

Gre-san
  • 351
  • 1
  • 5
  • The question is about `zsh`. But `bash` also has functionality the OP asks: `readline` has a function [`edit-and-execute-command`](https://www.gnu.org/software/bash/manual/html_node/Miscellaneous-Commands.html#index-edit_002dand_002dexecute_002dcommand-_0028C_002dx-C_002de_0029); by default it's bound to `C-x` + `C-e`. – phd Aug 07 '20 at 22:59
  • @gre-san It doesn't open `vim`; the Readline library provides a vim-like editing environment. `zle`, the `zsh` line editor, also provides a `vi`-like editing mode. – chepner Aug 09 '20 at 15:20
0

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.

Xen_mar
  • 8,330
  • 11
  • 51
  • 74