Vi Editing Mode for Bash

Setting up vi-mode in BashEver find yourself at the Bash prompt, typing out a very long series of arguments or parameters, only to hideously misspell something at the beginning on the line?

If you’re handy with the emacs editor, you already know the answer. You can use the emacs key bindings to go back and efficiently edit your command line. You don’t need to retype it, or tediously use the arrow keys to go back character-by-character.

However, what if you’re not handy with emacs bindings, but vi bindings? What then?

Fortunately, you can change the command line editing mode used by Bash. The default is emacs-mode, but you can easily change it to vi-mode.

This is accomplished by running the following at the Bash prompt:


set -o vi

Tada! Now you simply need to hit the ESC key, and you can edit the command line using many common vi bindings.

This includes cursor movement commands, such as: $ to move to the end of the line, 0 to move to the beginning of the line, w to move forward a word, of b to move backwards a word.

You can also use actions, such as: dw to delete a word, cw to change a word, i to enter insert mode, or r to replace a character.

Other cursor commands also work: j/k scrolls forwards and backwards in command history, while h/l move the cursor backwards and forwards a single character at a time.

Note that changing the editing mode to vi-mode will disable the default emacs keybindings such as Alt + Del. You can restore emacs-mode by running set -o emacs, or by starting a new terminal session.

While this isn’t necessarily a better way, it can be much more efficient for people (such as myself) who are more familiar with vi than with emacs.

Placing set -o vi in your .bashrc or .bash_profile file ensures you always have vi-mode editing available.

This also works on Mac OSX if you are using Bash as your shell.

Conversation
  • Tethys says:

    Put “set editing-mode vi” into ~/.inputrc instead of “set -o vi” in ~/.bashrc. That way you have sane history editing not just for bash, but for anything else that uses readline (such as psql, for example).

  • filterfish says:

    You are better off switching to zsh. readline is fairly broken especially when it comes to vi bindings. For example trying binging ctl-l in insert mode; it doesn’t work and there are numerous other examples.

    But vi bindings on the command line is most definitely a win. You might want to try setting the initial mode to command (you have to change to insert mode to type anything), it takes a while to get used to but it works well.

  • jcm says:

    f does not go forward a word. It searches for the next character typed. w goes forward a word.

  • lefty.crupps says:

    I prefer Vim for an editor over emacs, but at the command line it seems that the Vi keybindings would complicate things. What is wrong with arrow keys and the [Home] and [End] keys, which is such a common convention for pretty much every text editor (including this comment box)?

    Of course, use what you like, but using Vi keybindings doesn’t help to show our afraid-of-CLI friends that the CLI can be useful and not scary! :D

  • Justin Kulesza Justin Kulesza says:

    @jcm: Quite right — thanks for catching that. I’ve fixed it in the body of the post.

  • Comments are closed.