Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

migrated from stackoverflow.com Jul 13 '12 at 19:55

This question came from our site for professional and enthusiast programmers.

6 Answers

up vote 18 down vote accepted

You're talking about the greatest feature ever!

To use vi commands to edit your shell commands (and history) add 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!

share|improve this answer
Well, it's not really using vi, it's using vi-style keyboard commands for line editing. –  evil otto Jul 13 '12 at 19:00
Sure... I guess the appropriate terminology is that vi bindings in the shell are the greatest feature ever invented! –  jahroy Jul 13 '12 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 '12 at 21:58
They, is there a way to bind jj with ESC as we usually do in vim ? –  yangchenyun Jul 20 '12 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 '12 at 5:07
show 1 more comment

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.

share|improve this answer
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.). –  Jonathan Leffler Jul 13 '12 at 19:20
add comment

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.)

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
Wish I could upvote this 5 times for vi bindings in MySQL! –  jahroy Jul 20 '12 at 22:32
add comment

in your terminal , type export EDITOR=vi.

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

share|improve this answer
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). –  Keith Thompson Jul 13 '12 at 19:58
@KeithThompson Zsh defaults to vi editing mode if $EDITOR contains vi. I think it's the only shell that does this. –  Gilles Jul 13 '12 at 23:53
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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