Advantages of using set -o vi

por | 3 diciembre, 2020

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.

he Esc-v trick is one of the best features of set -o vi that most people don’t know. This is especially true if you want to issue the same command multiple times with different arguments (as multiple command line commands)

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