vim

Zsh Vim Mode

The Zsh Vim mode or Vi mode can be enabled by executing bindkey -v command or placing it in the zshrc file. Essentially, this command enables the Vi(m) editor key bindings for the Z-Shell. So, you can use the vim mode switching, and motion commands right in the terminal outside the Vim.

In this tutorial, I will be exploring the Zsh vim-mode, how to set it up in Zsh and Oh My Zsh, and other discuss options to enhance vim-mode in Zsh.

Note: Typically, vi mode term is used; in this guide, I will use both vi mode and vim mode interchangeably.

Zsh Vim Mode

You must be familiar with the typical functionality of Vim if you use it for editing, developing, or document management. Vim has various editing modes such as normal, insert, or command-line mode. Vim also has its own file navigation key bindings. If you want to employ the same experience within the Z-Shell, then it can be done using vi mode. These key bindings can be set for Z-Shell, Oh My Zsh, and Bash as well.

Note: To perform the instructions given in this guide, I am using Linux (Ubuntu 22.04) in which the latest Zsh is installed. Plugins installed in this guide are Linux-specific, so those commands will not work in macOS.

Enable Zsh Vim Mode

To enable the vim mode temporarily in Zsh on Linux, or macOS, execute the bindkey -v command.

bindkey -v

To set up the permanent vim mode in Zsh, first, open the zshrc file.

There are two zshrc files in Unix-like systems, one is system-wide in the /etc directory and the other is user-defined in the home directory. I recommend using the user-specific zshrc file, if you cannot find it in the home directory, you can create it by using touch ~/.zshrc command.

touch ~/.zshrc

Now, open the zshrc file in any text editor; I am opening it in Vim editor.

sudo vim ~/.zshrc

Put the bindkey -v in the zshrc file.

bindkey -v

When you switch from insert mode to normal mode, there might be a little delay. To fix it, set the key time out to 1 in the zshrc file.

KEYTIMEOUT=1

Now, save and quit the file using the :wq command or pressing the shift+zz keys.

Now, apply changes by sourcing the zshrc file using the following command.

source ~/.zshrc

Restart the Zsh, and Zsh vim-mode is enabled now.

Note: The zshrc file is hidden, to view the hidden files in the home directory use the ls -a command. All the filenames starting with a dot are hidden files.

Using Zsh Vim Mode

There is no special indication about the vim mode enabling in the Zsh; you have to experience it. The i key can be used to enable the insert mode, and the Esc key can be used to return to the default or normal mode.

The Zsh vim mode is important, especially in modifying the lengthy commands. For example, if you forget to add the sudo at the beginning of the line. Simply, press Esc to enter the normal mode and then the 0 motion command to get the beginning of the line. Now, you can type the sudo again by entering the insert mode.

Moreover, to select the text, press v to enter the visual mode. To delete the word press diw, to yank the word press yiw. Overall, you can use all the Vim commands quite conveniently.

dd To delete the command
yy To yank the command
vv To open the command in the Vim editor for editing
/ or ? To search the commands in the history
[count]x To delete the characters
cc Delete the command and enable the insert mode
: To execute the vi-mode commands

Note: If you are using vim mode, then over 400 vim-mode commands can be listed in the Zsh by pressing colon : and then the Return keys.

Enhance Zsh Vim Mode through Zsh-Vi-Mode Plugin

The default vim mode in Zsh is good but lacks many important features. For example, there is no indication of mode switching, which can be frustrating. To quickly fix it, I would advise installing the Zsh-Vi-Mode plugin.

To get this plugin, you must have a Zsh plugin manager installed. There are many Zsh plugin managers such as Antigen, ZPlug, Zgen, Zinit, and Zap. For this tutorial, I will install the Antigen plugin manager for Zsh.

There are different methods to install the Antigen plugin manager. Ubuntu has a default package manager called APT that I can use to install it.

sudo apt install zsh-antigen

To install it on macOS use:

brew install antigen

To learn other installation methods, visit here.

After the installation of the plugin manager, I will install the Zsh Vi Mode plugin. Open the zshrc file and place the following lines in it.

source $HOME/antigen.zsh

antigen bundle jeffreytse/zsh-vi-mode

antigen apply

To complete the installation process of the plugin, source the zshrc file.

source ~/.zshrc

Now, the plugin is installed; so, when you are in the normal mode the cursor will be in block style (▊) while in insert mode the cursor will be in beam style (▏). Moreover, the text selection will be in red. However, all of these settings can be customized thanks to the plugin.

To learn more about the functionalities, features, and customization options, read here.

Enable Vim Mode in Oh My Zsh

Zsh is popular because of its customizability and advanced features. If you are Zsh user, then you must have quickly switched to the Oh My Zsh; an open-source project to enhance the functionality of vanilla Zsh.

The customization options in Oh My Zsh are endless, making it an immediate choice for Zsh users. You can also enable the vim-mode in Oh My Zsh using the vi-mode plugin. The Oh My Zsh has a vi mode plugin, which gives additional features apart from the default vi mode options, such as Vim mode indicator, various cursor styles, and other customization options.

Open the zshrc file in the Vim editor.

sudo vim ~/.zshrc

Place the vi-mode in the plugins option.

plugins=(vi-mode)

Now, save and exit the file using :wq command and source it.

source ~/.zshrc

Now, you will get various indications for modes, and selection. You can also modify the cursor indication for various modes.

The normal is indicated by <<< on the right side of the terminal window, while the insert mode indication is disabled by default, but it can be enabled by specifying it in the zshrc file.

INSERT_MODE_INDICATOR="%F{white}+%f"

Now, the + sign will appear as the insert mode indication. The color and the indicator sign can be modified.

Read more about the customization options of this plugin from here.

Disable the Vim Mode in Zsh

In some situations, the vim mode can be a hurdle. Luckily, it can be disabled using bindkey -e command in the Zsh.

bindkey -e

This command enables the default Emacs key bindings, but temporarily for the current session. To make it permanent, remove the bindkey -v command and replace it with bindkey -e in the zshrc file.

Conclusion

The vim mode in Zsh enables the Vim-like key binding in the Z-shell. It allows us to edit and navigate the commands using the Vim key binding. It can be super beneficial for a regular Vim user. Furthermore, it can easily be enabled by placing bindkey -v command in the zshrc file. The vanilla vim mode lacks many functionalities, but this gap can be filled with the help of plugins. The vim mode is also available for Oh My Zsh; again, Oh My Zsh plugins enhance the basic vim mode experience.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.