Now Reading: Increasing your productivity with zsh-completions

Loading
svg
Open

Increasing your productivity with zsh-completions

January 15, 20243 min read

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

Zsh comes with a variety of productivity enhancing features such as completion. Zsh completion is a feature that provides intelligent and context-aware tab completion. They help save you time and reduce typing errors by suggesting and autocompleting commands, file paths, parameters, and more.

In this tutorial, we will discuss the functionality and usage of Zsh completions in depth. We'll start with the basics and move on to more advanced customization and usage.

installing zsh

Before diving into Zsh perfection, make sure Zsh is installed on your system. To check if Zsh is installed, run the following command:

If you don't have Zsh installed, you can quickly set it up using the package manager for your system. Use apt on a Debian-based system as follows:

, sudo apt update
, sudo apart to install zsh

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

oh my zash installation

The next component we need to use zsh completions is oh my ZSH. OMZ is a popular framework for managing Zsh configuration 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 Oh My ZSH Framework on your system. This creates a default “.zshrc” file in your home directory where all the configuration for your Zsh is contained.

You can edit and modify this file as per your liking and include or exclude any features as you wish.

Installing zsh completions

Depending on the version of Oh My Zsh you have installed, you will have Zsh completions automatically installed.

However, if you haven't configured it already, run the following command to clone the plugin:

, git clone https:,github.com,zsh-user,zsh-completions ${ZSH_CUSTOM:-${ZSH:-~/.oh-my-zsh},custom,,plug-in,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 create the source of 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 command names, options, and file paths.

The following example shows an example of command completion in Zsh:

$cl<tab,
# Autocomplete:
, clear

We can do this for parameter completeness as well. Take the following example:

, Ras -l –Associate<tab,

# Autocomplete:

, Ras -l –Colour

command completion

We can also define custom completions for different commands. For example, let's say we have a script named “netprobe.sh” that has subcommands named “dns” and “vhosts”.

We can create completions for subcommands by defining a function in the “~/.zshrc” file as follows:

_netprobe,, ,
Case $state In
,Order,
_logic “1:->subcommand” ,
,sub command,
Case $word[1] In
,DNS,
_logic “–port[Port number]: :_Ports” ,
,vhosts,
_logic , ,
esac
,
esac
,

In the given example, we start by defining a function called “_netprobe” to handle the completion of the “netprobe” command.

Command status handles completion for the main command which is “netprobe”.

Next, we define the subcommand conditions which are “dns” and “vhost” to facilitate completing the subcommand.

We finally use “_arguments” to specify the expected arguments for each subcommand.

To activate custom completion, we can add “compdef_netprobe” netprobe to “.zshrc” file.

customizing perfections

As you can guess, Zsh completions are highly customizable making it very easy to tune everything to your liking.

The following are some common customization options you can use:

# Custom completion for `git` commands.
_git,, ,
_logic “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:*' Option '(-a)-a' '(-F)-F' '(-ji)-ji' '(-H)-H'

In this syntax, we define custom completion for the “ls” command.

fuzzy matching

There are some advanced features that you can apply when you are done. One such feature is fuzzy searching which allows support for regular expressions such as the format of command completion.

To enable fuzzy search, add the following entry to the “.zshrc” file:

zstyle ':ending:*' matchlist 'm:{a-zA-Z}={A-Za-z}'

Enabling this option allows Zsh to match commands and files even if there are typos or different wrappers.

autocomplete hostname – ssh

You can also allow Zsh to autocomplete remote hostnames, such as when dealing with SSH.

Add entry like this:

zstyle ':Completion:*:Host' host $,Cat ,,.ssh,known_host,

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 about the autocomplete feature in Zsh.

How do you vote?

0 People voted this article. 0 Upvotes - 0 Downvotes.
svg

What do you think?

Show comments / Leave a comment

Leave a reply

Loading
svg