Zsh comes with a wide variety of productivity boosting features such as completions. Zsh completion is a feature that provides intelligence and context-aware tab completions. They help you save time and reduce the typing errors by suggesting and auto-completing the commands, file paths, parameters, and more.
In this tutorial, we will discuss in-depth the functionality and usage of Zsh completions. We will start with the basics and proceed to a more advanced customization and usage.
Installing Zsh
Before diving into Zsh completions, ensure that Zsh is installed on your system. To check if Zsh is installed, run the following command:
If you do not have Zsh installed, you can quickly set it up using the package manager for your system. Use apt on Debian-based system as follows:
$ sudo apt install zsh
On macOS, Zsh comes as the default shell. But to confirm, you can use Homebrew to install it as follows:
Setting Up Oh My Zsh
The next component that we need in order to use the Zsh completions is Oh My ZSH. OMZ is a popular framework for managing the Zsh configurations and adding plugins.
To install it, run the following command:
This should download and install the Oh My Zsh framework on your system. It creates a default “.zshrc” file in your home directory where all the configurations for your Zsh lies.
You can edit and modify this file to your liking to include and exclude any features that you wish.
Installing Zsh Completions
Depending on the version of Oh My Zsh that you installed, you will have the Zsh completions installed automatically.
However, if you do not have it already configured, run the following command to clone the plugin:
Once cloned, edit the “.zshrc” file and add the following entry to the “.zshrc” file:
Save the file and source the new configuration to apply the changes:
Basic Tab Completion
Zsh provides a basic tab completion out of the box. For example, we can use it to complete the command names, options, and file paths.
The following example shows the example of command completion in Zsh:
# auto-complete:
$ clear
We can also do the same for parameter completion. Take the following example:
# auto-complete:
$ ls -l --color
Command Completions
We can also define the custom completions for various commands. For example, suppose we have a script called “netprobe.sh” with the subcommands called “dns” and “vhosts”.
We can create the completions for the subcommands by defining a function in the “~/.zshrc” file as follows:
case $state in
(commands)
_arguments "1: :->subcommand" ;;
(subcommand)
case $words[1] in
(dns)
_arguments "--port[Port number]: :_ports" ;;
(vhosts)
_arguments "" ;;
esac
;;
esac
}
In the given example, we start by defining a function called “_netprobe” to handle the completions for the “netprobe” command.
The command state handles the completion for the main command which is “netprobe”.
Next, we define the subcommand state to facilitate the completions for the subcommands which are “dns” and “vhosts”.
We finally use the “_arguments” to specify the expected arguments for each subcommand.
To activate the custom completion, we can add the “compdef_netprobe” netprobe to the “.zshrc” file.
Customizing the Completions
As you can guess, Zsh completions are highly customizable which makes it very easy to tune everything to your liking.
The following are some common customization options that you can use:
_git() {
_arguments "1: :($(git --help | grep -o ' [a-z]' | tr -d ' '))"
}
compdef _git git
The given example defines a custom completion for the “git” command. You can use this format for other commands in your system.
In this syntax, we define the custom completions for the “ls” command.
Fuzzy Matching
There are some advanced features that you can implement when it comes to completions. One such feature is fuzzy searching which allows support for regular expression like the format of command completion.
To enable the fuzzy search, add the following entry on the “.zshrc” file:
Enabling this option allows Zsh to match the commands and files even when there is a typo or different casing.
Autocomplete Hostnames – SSH
You can also allow Zsh to autocomplete the remote hostnames such as when dealing with SSH.
Add the entry as follows:
This command uses the “known_hosts” file in the “.ssh” directory to know the available hosts and the autocomplete feature.
Conclusion
In this tutorial, you learned everything you need to know when it comes to the autocomplete feature in Zsh.