Skip to Content

How to Use Vim Mode in Bash Command Line

  • Vim mode in Bash allows you to use Vim keybindings and commands to edit your commands more efficiently.
  • You can enable and customize Vim mode in Bash by using the set and bind builtin commands, and adding them to your .bashrc file.
  • You can also use some tips and tricks to make the most of Vim mode in Bash, such as using the v and fc commands, the history expansions, and the INPUTRC environment variable.

Bash is the default shell in most Linux distributions and it provides a powerful and flexible way to interact with your system. However, if you are used to using Vim as your text editor, you may find the default command line editing mode of Bash (which is based on Emacs) to be unfamiliar and inconvenient.

Fortunately, Bash supports an alternative editing mode that mimics the behavior of Vim, allowing you to use Vim keybindings and commands to edit your commands more efficiently. In this article, we will show you how to enable and customize Vim mode in Bash command line, and some tips and tricks to make the most of it.

How to Use Vim Mode in Bash Command Line

Enabling Vim Mode in Bash

To enable Vim mode in Bash, you need to use the set builtin command with the -o option and the vi argument. This will switch the editing mode of Bash from the default Emacs mode to Vim mode. You can do this in your current Bash session by typing:

set -o vi

Alternatively, you can make this change permanent by adding the above command to your .bashrc file, which is a configuration file that Bash executes every time you start a new session. To do this, you can use your preferred text editor to open the .bashrc file in your home directory and append the set -o vi command at the end of the file. For example, using Vim itself, you can type:

vim ~/.bashrc

Then, press G to go to the end of the file, press o to start a new line in insert mode, type set -o vi, press Esc to go back to command mode, and type :wq to save and quit the file.

After enabling Vim mode in Bash, you will notice that the command line behaves differently. For example, you can no longer use the arrow keys to move the cursor around, but you have to use the hjk, and l keys instead. This is because Vim mode has two sub-modes: command mode and insert mode. Command mode is the default mode, where you can use Vim commands to edit the command line. Insert mode is where you can type text normally. You can switch between the two modes by pressing Esc (to go to command mode) and i (to go to insert mode).

Customizing Vim Mode in Bash

Vim mode in Bash is not exactly the same as Vim itself. It is a simplified version that only supports a subset of Vim features and commands. However, you can customize Vim mode in Bash to some extent by using the bind builtin command, which allows you to bind keys or key sequences to readline functions. Readline is the library that Bash uses to provide command line editing features. You can use the bind command in your current Bash session, or add it to your .bashrc file to make it persistent.

For example, one common customization is to show the current mode (command or insert) in the prompt, so that you don’t get confused about which mode you are in. To do this, you can use the show-mode-in-prompt readline variable, which adds a + sign for insert mode and a : sign for command mode at the beginning of the prompt. You can enable this variable by typing:

bind 'set show-mode-in-prompt on'

Or, you can add the following line to your .bashrc file:

bind 'set show-mode-in-prompt on' 2>/dev/null

The 2>/dev/null part is to suppress any error messages that may occur if you run this command in a non-interactive shell.

Another common customization is to change the cursor shape according to the mode, so that you can visually distinguish between the two modes. For example, you can use a beam cursor for insert mode and a block cursor for command mode. To do this, you need to use the vi-ins-mode-string and vi-cmd-mode-string readline variables, which define the strings to be sent to the terminal when entering insert mode and command mode, respectively. For example, you can type:

bind 'set vi-ins-mode-string \1\e[6 q\2'
bind 'set vi-cmd-mode-string \1\e[2 q\2'

Or, you can add the following lines to your .bashrc file:

bind 'set vi-ins-mode-string \1\e[6 q\2' 2>/dev/null
bind 'set vi-cmd-mode-string \1\e[2 q\2' 2>/dev/null

Note that this customization may not work on some terminals that do not support changing the cursor shape.

Tips and Tricks for Using Vim Mode in Bash

Here are some tips and tricks for using Vim mode in Bash more effectively:

  • You can use the v command in command mode to open the current command line in your $EDITOR, which is usually Vim or a variant of it. This way, you can use the full power of Vim to edit your command, and when you save and quit the file, the command will be executed in Bash. This is useful for editing long or complex commands that are hard to edit in the command line.
  • You can use the fc builtin command to edit the previous command in your $EDITOR. This is similar to the v command, but it works for any command in your history, not just the current one. You can also specify a range of commands to edit by using the -l option and the history numbers. For example, fc -l 10 20 will edit the commands from 10 to 20 in your history. You can also use the -r option to reverse the order of the commands. After editing the commands, you can either execute them or save them to a file by using the -w option and a filename.
  • You can use the !! and !$ history expansions to repeat the previous command and the last argument of the previous command, respectively. For example, if you type ls -l /etc, you can type !! to run the same command again, or !$ to use /etc as the argument for another command, such as cd !$. You can also use the !^ history expansion to use the first argument of the previous command, or !:n to use the nth argument of the previous command, where n is a number starting from 0. For more information about history expansions, see the Bash manual.
  • You can use the C-r key combination in command mode to search your history for a command that matches a pattern. For example, if you type C-r ls, you will see the most recent command that contains ls in it. You can press C-r again to see the previous matching command, or C-s to see the next matching command. You can also edit the command before executing it by pressing Esc and using Vim commands. To cancel the search, press C-g.
  • You can use the C-a and C-e key combinations in command mode to move the cursor to the beginning and end of the line, respectively. These are the same key combinations that are used in Emacs mode, and they are convenient shortcuts for the Vim commands 0 and $.
  • You can use the C-l key combination in command mode to clear the screen and redraw the prompt. This is the same as typing the clear command, but it does not create a new entry in the history.

Frequently Asked Questions (FAQs)

Question: How do I disable Vim mode in Bash?

Answer: You can disable Vim mode in Bash by using the set builtin command with the -o option and the emacs argument. This will switch the editing mode of Bash back to the default Emacs mode. You can do this in your current Bash session by typing:

set -o emacs

Alternatively, you can remove the set -o vi command from your .bashrc file if you have added it before.

Question: How do I customize the Vim keybindings in Bash?

Answer: You can customize the Vim keybindings in Bash by using the bind builtin command, which allows you to bind keys or key sequences to readline functions. Readline is the library that Bash uses to provide command line editing features. You can use the bind -P command to list the current keybindings, and the bind -l command to list the available readline functions. You can also use the bind -m vi command to list the keybindings specific to Vim mode. For example, to bind the C-d key to the delete-char function in Vim mode, you can type:

bind -m vi-insert "\C-d: delete-char"

Or, you can add the following line to your .bashrc file:

bind -m vi-insert "\C-d: delete-char" 2>/dev/null

The 2>/dev/null part is to suppress any error messages that may occur if you run this command in a non-interactive shell.

Question: How do I use Vim commands in other programs that use readline?

Answer: You can use Vim commands in other programs that use readline by setting the INPUTRC environment variable to a file that contains the set editing-mode vi command. The INPUTRC variable specifies the name of the file that readline uses to initialize itself. For example, you can create a file named .inputrc in your home directory and add the following line to it:

set editing-mode vi

Then, you can set the INPUTRC variable to point to this file by typing:

export INPUTRC=~/.inputrc

Or, you can add the above command to your .bashrc file to make it permanent. This way, any program that uses readline, such as Python, Ruby, or MySQL, will use Vim mode for command line editing.

Summary

In this article, we have learned how to use Vim mode in Bash command line, which allows you to use Vim keybindings and commands to edit your commands more efficiently. We have also learned how to enable and customize Vim mode in Bash, and some tips and tricks to make the most of it. Vim mode in Bash is a useful feature for Vim users who want to have a consistent and productive command line experience.

Disclaimer: The information in this article is based on the Bash version 5.1 and the Vim version 8.2. The behavior of Vim mode in Bash may vary depending on the versions of Bash and Vim that you are using, as well as the configuration of your system and terminal. Therefore, we recommend that you test the commands and settings in this article before using them in your daily work. We are not responsible for any damage or loss that may result from following this article. Use Vim mode in Bash at your own risk.