Utilize Vi Keyboard Shortcuts in Your Terminal

You can configure your terminal to use Vi(m) keyboard shortcuts. This will work for the Bash shell and most command line applications including the Interactive Ruby Shell (irb), and MySQL’s command line client.

Here is how you get it to work:

add ‘set -o vi’ to ~/.bashrc (if you use the Bash shell)

add ‘bind -v’ to ~/.editrc

add ‘set editing-mode vi’ to ~/.inputrc

You may need to create the above files if they don’t already exist.

You start in insert mode; as usual press ‘esc’ or ‘ctr-[‘ to enter command mode.

How it works

The line, ‘set -o vi,’ is invoking the Bash built-in command ‘set’. ‘set’ is used to configure Bash options. In this case, it is telling Bash to use the vi line editing interface. You can read the GNU Bash manual for more information. (Take a look at your shell’s documentation for vi keyboard configuration if you don’t use the Bash shell.)

The line, ‘bind -v,’ is used to configure the Command Line Editor library–see the ‘editline‘ and ‘editrc‘ man pages. It tells the library to bind all keys to the standard vi key bindings. This will configure applications that use the Command Line Editor library.

The last line, ‘set editing-mode vi,’ is used to configure the Readline library. It configures the library to use vi editing mode. Any applications compiled with Readline support will now use the vi key bindings. You can read more about the Readline init file here.