When working in the terminal, it is good to train yourself to only use the keyboard. Removing the need to rely on the mouse can help increase your productivity up to 50%. This is because it reduces the time required between switching the inputs and locating the target element that you wish to use.
In ZSH, we can take advantage of the keyboard features using the custom key bindings. Key bindings in ZSH allows us to define the custom keyboard shortcuts to perform the specific tasks in the shell.
An example includes the key binding to search the keyboard history and more. In this tutorial, will learn how to create the custom key binding in ZSH using the “bindkey” command.
Requirements:
Before we dive into the world of custom key bindings in ZSH, ensure that you have the following requirements met:
- A working installation of ZSH
- Familiarity with basic ZSH configuration and shell usage
- A text editor to edit your ZSH configuration files (e.g., ~/.zshrc)
With the given requirements being fulfilled, we can proceed with the guide.
Basics of ZSH Key Bindings
Let us begin by learning the fundamentals of key bindings in ZSH.
To create a custom key binding in ZSH, we use the “bindkey” command. Key bindings are also known as keymaps. We usually define the custom key bindings in the ZSH configuration file.
The following shows the basic syntax to create a custom key binding in ZSH configuration file:
The given command entails the following elements:
- key – This defines the key or key sequence to bind.
- sequence – This sets the key sequence to activate the binding.
- command – This defines a command or function to be executed when the binding is triggered.
Basic Example
Let us look at the basic example. Suppose we wish to bind the “CTRL + X” key shortcut to run the “exit” command which terminates the shell.
We can create a key binding in the ZSH configuration file as follows:
In this case, we start by calling the “bindkey” command which tells ZSH that we wish to create a key binding.
We then provide the key sequence that we wish to use. In this case, the “^X” format represents “Ctrl-X”.
Finally, we specify the command that we wish to execute when we press the specified key binding. In our case, we specify the command as “exit\n”. The “\n” simulates the “Enter” key.
We can save the configuration file and source it to apply the changes as follows:
You can test out the key binding by pressing “CTRL + X”. This terminates your current session.
ZSH Key Binding Modes
ZSH supports different key binding modes, each designed for specific scenarios. The main modes include:
- emacs – It emulates the emacs-style key bindings.
- viins – It provides the key bindings that are similar to those in Vi’s insert mode.
- vicmd – It provides the key bindings that are similar to those in Vi’s command mode.
To switch between these modes, we can use the “bindkey” command with the -e, -v, or -a flags, respectively. By default, ZSH uses the “emacs” mode.
To switch to Vim-style key bindings, we can switch to “vicmd” mode by adding the following line in the ZSH configuration file:
Add the following line:
ZSH Key Unbinding
To unbind a key or key sequence, we can use the “bindkey –r” command followed by the key or sequence that we wish to unbind. For example:
The given example entry unbinds the “Ctrl-X” key sequence if it is previously bound.
Example 1: Command Correction
We can create a key binding to automatically correct an incorrect command. For example, suppose you constantly mistype the “clear” command to “clera”.
You can create a custom key binding that automatically fixes this mistake as shown in the following example:
Once we reload the changes, typing the “clera” command automatically forces ZSH to correct the typo and run the “clear” command instead.
Example 2: Word Navigation
We can create the custom key bindings to navigate the command line more efficiently. For instance, we can bind “Alt-Left” and “Alt-Right” to move the cursor word by word:
bindkey -s '^[[1;3C' 'forward-word\n'
In this example:
- ^[[1;3D corresponds to Alt-Left
- ^[[1;3C corresponds to Alt-Right
Example 3: Running the Custom Function
We can also create a key binding that runs a custom function. For example, suppose we want to have a key shortcut that quickly prints the current date and time.
We can define the function in the ZSH configuration file as follows:
Creating a custom function:
date '+%Y-%m-%d %H:%M:%S'
}
We can then bind this function to a specific key sequence as shown in the following example:
In this key, pressing the “CTRL + T” key sequence runs the “show_datetime” function and displays the current date and time.
Conditional Key Bindings
ZSH also allows us to create the conditional key bindings based on the context of the shell session. For example, suppose we wish to bind the key sequence of “CTRL + D” based on different actions on whether we are at the beginning of a line or not.
We can achieve this using the ZLE widgets and the “precmd” function. Consider the following example:
if [[ $BUFFER == "" ]]; then
bindkey -s '^D' 'exit\n'
else
bindkey -s '^D' 'delete-char\n'
fi
}
In this case, “CTLR + D” acts as exit if the command line is empty and as delete-char otherwise.
Key Bindings with Arguments
To extend the functionality of key bindings, ZSH allows us to specify the key bindings that accept the arguments.
For example, suppose you want to create a binding to the “find” command while providing the file that the command should search.
We can create an example binding as follows:
read -q "search?Enter file name to search in ~/ (wildcards allowed): "
[[ -n $search ]] && find ~/ -type f -iname "*$search*"
}
zle -N find_in_home
bindkey '^F' find_in_home
In this example, if we press the key sequence of “Ctrl-F”, the shell prompts us for a file name to search in the home directory.
Conclusion
In this extensive tutorial, we learned everything you need to know when it comes to key bindings in ZSH. This taught you the basics of the “bindkey” command to extensive features such as argument parsing, conditional key bindings, and more.