I used to have a co-worker who was really good at UNIX.

He showed me how to use Vi key bindings to edit my shell commands.

He placed the command in a file that ran every time I logged in.

Since then, I've moved to a different project.

Unfortunately I don't remember how to set this up.

Is there anyone here who knows how to use Vi key bindings to edit commands in the terminal?

How can I make that setting permanent?


6 Answers 6


You're talking about the greatest feature ever!

You can use vi commands to edit shell commands (and command history) by adding this to your .bashrc file:

set -o vi

You can also run that command from the command line to affect only your current session.

If you don't use bash, substitue the appropriate rc file for your shell.

This allows you to use vi commands to edit any command...

You can also use j and k to move through your history (after pressing ESC).

You can also use / (after hitting ESC) to search for old commands.

In other words, to find that super-long cp command you did ten minutes ago:


Then you can cycle through all the matching commands in your history with n and N.

All this makes me 10 trillion times more productive at the command line!

  • 4
    Well, it's not really using vi, it's using vi-style keyboard commands for line editing.
    – evil otto
    Jul 13, 2012 at 19:00
  • 4
    Sure... I guess the appropriate terminology is that vi bindings in the shell are the greatest feature ever invented!
    – jahroy
    Jul 13, 2012 at 19:01
  • I would switch from emacs bindings to vi bindings if there were a way to display the mode. Do you know of a way to do that?
    – iconoclast
    Jul 19, 2012 at 21:58
  • 1
    They, is there a way to bind jj with ESC as we usually do in vim ?
    – steveyang
    Jul 20, 2012 at 4:55
  • @yangchenyun - Maybe this question will help with that: stackoverflow.com/questions/844862/… I don't personally bind/alter any of the commands, so I'm not quite sure what you're asking.
    – jahroy
    Jul 20, 2012 at 5:07

If you're using bash, as jahroy and evil otto have already answered, you can use

set -o vi

to cause bash to use vi-style editing commands.

Once you've done this, you can type Esc v to launch the vi editor with a temporary file containing a copy of the current command line. You can edit the command, even replacing it with multiple lines; when you save the file (:wq), the shell will execute the edited commands.

If you prefer

set -o emacs

you can use Ctl-XCtl-E to do the same thing. It will use your preferred editor $EDITOR, not necessarily emacs.

(Personally, I use vi (actually vim) for most of my editing, but I prefer set -o emacs in the shell; switching in and out of insert mode is great for editing files, but awkward for interactive commands. YMMV.)

  • do you know the way to return back to normal editing mode avoiding vi-style? I remember succeeding with Bl Num, but now no more; what am I missing?
    – piertoni
    Jan 15, 2017 at 12:30
  • @piertoni: Does set -o emacs not do what you want? Jan 15, 2017 at 20:33
  • Yes, It does work, I missed the point, thanks
    – piertoni
    Jan 17, 2017 at 8:53

You can use set -o vi to change your line editing commands as @jahroy posted, but you may be thinking of the fc command (available in bash and I think ksh, but probably not tcsh), which will put the previous command into an editor (FCEDIT or EDITOR, which you probably have set to vi) and then executes the command when you exit the editor. See the manpage or help fc for details, or just try it out.

  • 5
    alias r='fc -e -' is a valuable alias to rerun previous commands. fc on its own originated in Korn shell, I believe. You can specify commands by number (fc 123 129 to edit command numbers 123 to 129 in the history). You can specify relative numbers (fc -10 -1 to edit the last 10 commands entered). You can list previous commands with fc -l. You can specify a command by prefix with the r alias (r make runs the last invocation of make; so does r m if you've not run a mv command since you last ran make, etc.). Jul 13, 2012 at 19:20

To edit most recently used command in vim and invoke an edited version after editor saves and exits, use fc shell built-in. Without any arguments it will do the following:

  1. Creates a temporary file in /tmp and populates it with the most recently typed command
  2. After editor exits normally (with exit code 0) it executes command found in that temporary file and removes this file. Edited version is saved in history as a usual command (at least in zsh). It executes nothing if editor exits with code other then zero (in vim this can be achieved by either killing it or using :cquit).

Command works both in bash and zsh.


Indeed, this is in fact the greatest feature ever. There's more though:

Create a file named


containing the line

bind -v

Editline, is a BSD licensed version library that provides readline-like services. That .editrc will set your keybindings to vi mode in MySQL's command line shell, or any application that does not use readline.

I have grown somewhat forgetful with the passing of years, so my .zshrc sources some wrappers for vi mode, to show me what mode I am in on the command line. It's nice.

I know in zsh you can type

bindkey -L

and get the list of keybindings. I'm sure bash must have something similar. Not all of the vi keys are bound by default. Bind them how you see fit and you are off to the races.

  • Wish I could upvote this 5 times for vi bindings in MySQL!
    – jahroy
    Jul 20, 2012 at 22:32

in your terminal , type export EDITOR=vi.

To save this setting . put this line in the file ~/.profile

  • 5
    That sets your default editor (for commands that pay attention to $EDITOR) to vi. It doesn't let you edit shell commands with vi (at least not directly). Jul 13, 2012 at 19:58
  • 2
    @KeithThompson Zsh defaults to vi editing mode if $EDITOR contains vi. I think it's the only shell that does this. Jul 13, 2012 at 23:53

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.