zsh

Boosting Your Productivity with Zsh-Completions

We are all familiar with Zsh. It is an free and open-source shell interpreter that is similar to Bash but with more features and high level of customization.

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:

$ zsh --version

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 update
$ sudo apt install zsh

On macOS, Zsh comes as the default shell. But to confirm, you can use Homebrew to install it as follows:

$ brew install zsh

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:

$ sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

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:

$ git clone https://github.com/zsh-users/zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh}/custom}/plugins/zsh-completions

Once cloned, edit the “.zshrc” file and add the following entry to the “.zshrc” file:

fpath+="${ZSH_CUSTOM:-"$ZSH/custom"}/plugins/zsh-completions/src"

Save the file and source the new configuration to apply the changes:

$ source .zshrc

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:

$ cl<TAB>
# auto-complete:
$ clear

We can also do the same for parameter completion. Take the following example:

$ ls -l --co<TAB>

# 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:

_netprobe() {
  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:

# custom completion for the `git` command.
_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.

zstyle ':completion:*:*:ls:*' options '(-A)-a' '(-F)-f' '(-G)-g' '(-H)-h'

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:

zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}'

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:

zstyle ':completion:*:hosts' hosts $(cat ~/.ssh/known_hosts)

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.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list