Sign up ×
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 have seen many developers using this command to set the option to vi. I never understood the real use of this?

When using bash commands, what help does switching to vi provide?

share|improve this question

8 Answers 8

up vote 27 down vote accepted

By setting your readline editing to either emacs (the default) or vi (set -o vi) you are essentially standardizing your editing commands, across the shell and your editor of choice1.

Thus, if you want to edit a command in the shell you use the same commands2 that you would if you were in your text editor. This means only having to remember one command syntax and (if that were not advantage enough) would probably make your editing in both environments faster and less error prone...

You can further leverage this relationship in vi-mode by pulling up any command from your shell history, hitting Escape to enter command mode and then hitting v, which will open your $EDITOR with the command loaded for more complex editing with the full power of vim. Once you have finished editing the command to your satisfaction, :wq and the command is executed back in your shell.

1. Assuming, of course, that you use Emacs or Vi/m as your editor.
2. Or, more accurately, a subset thereof...

share|improve this answer

I'm not sure if there is a direct advantage. I've been a vi user for more than 20 years. I'm also a screen user for even longer, and of other programs that use vi keys. It's natural for me to prefer to set "vi" mode in bash. But I also work on hundreds of servers in my job, most are set to the default "emacs" mode. So I need to use both modes. But it is really just a matter of preference.

share|improve this answer
Similar situation for me - I use vim a lot but I've always found it less work to become proficient at the default emacs-like readline keys (which can be just as convenient as the vi subset) than it is to add a line switching to the vi mode on every new system I come across. – jw013 Jan 31 '12 at 20:52

The main advantage is modal editing of your command line. If you're familiar with Vim and likes its philosophy the benefits must be obvious. If you are experienced with it, your finger's muscle memory will make you edit your bash commands in lightning speeds.

NB: If you don't like modal editing, you should still learn to take advantage of the (default) emacs-mode. Here are some nifty keyboard shortcuts that will work on any process with readline, like bash.

share|improve this answer

It lets you edit stuff at the command line using the vi modes and operations.

An example will help make it much clearer:

You type cp tmp/some_other_long_directory/file1.xt /tmp2/some_other_xtra_long_dir/

but you get an error - you should have typed file1.txt not file1.xt

Without this option set, you press up-arrow and then press left arrow and let it repeat for... 35 times, until you get to the .xt and then you type the extra t. Total keystrokes: 37.

With this option set you can (for example) press arrow up once, then Escape for command mode, 0 to go to the start of the line and then /xt[return] to get to the xt and then you can type i for insert mode and type the missing t. This may seems insanely complicated in some respects but if you are a vim user these command are already very well known. Total keystrokes: 9

share|improve this answer
You could use a few Alt+Bs or a Ctrl+Alt+] . in Emacs mode too, so this doesn't seem like a strong example to me. – Mikel Apr 1 '14 at 0:11
Seeing as we're golfing: Emacs mode: Up, Ctrl+Alt+], ., Right, t, Enter = 8. Vi mode: Up, F, ., a, t, Enter = 7. :) – Mikel Apr 1 '14 at 0:44

Vi mode is a huge usability improvement if you are using a mobile SSH client like ConnectBot for Android.

This is due to a reduced reliance on modifier keys.

Vim is much easier to use with a virtual keyboard on a smartphone or tablet than ... anything else, including the native editing methods built into the Android UI. Ironically, it is easier to edit C sources with Vim in an SSH session than to edit, say, an instant message.

Shell vi mode brings a similar benefit.

share|improve this answer

If you get used to vi, so you set it to vi mode as your shell editor. That'd be the obvious reason. The other one is when the bash is not available by default in some OSes (mostly UNIX like AIX, Solaris), so the shell history feature is not available, so the way you get the past commands by setting the shell editor to vi, and Esc, Ctrl+K or Ctrl+L

share|improve this answer

It can let you easily navigate and edit the command line using vim's shortcuts, e.g. quickly move to one word right, delete a word.

By the default shortcuts, when you need to go to the end of the line, you need to Ctrl+e, whereas with set -o vi, you just hit $, like in vim.

share|improve this answer

Probably late to the party, but for me using the vi mode is more about when creating interactive scrips.. for example

for i in `ls | grep -v gz`
    echo $i
    gzip $i

a very simple example of what could be quite complicated.. using ESCkv puts you into a vi session where you can modify the script, and then :wq and it runs.

share|improve this answer
This may be a good example of using bash in vi mode, but it's a bad example of bash code.  (1) Using the output from ls as the input to any other sort of processing is a bad idea.  (2) The $(…) syntax for command substitution is widely considered to be more readable than the `…` syntax.  (3) You should always quote shell variables unless you have a good reason not to, and you’re sure you know what you’re doing. – G-Man Sep 5 at 18:13

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.