5 Types Of ZSH Aliases You Should Know

  • 时间: 2020-05-27 08:36:41

My shell means everything to me. It is the first program I start in the morning and the last one I close in the evening. It is the program that I regularly tweak to boost my overall productivity. From my perspective, Microsoft’s release of Windows Terminal 1.0 and WSL 2.0 will boost overall Shell adoption. Over the past decade, I have seen numerous users installing and using different shells. However, the number of people that invest time in configuring their shell to be more productive is just a small percentage. And that is a shame!

You can also stick with tools like Finder or Windows Explorer if you install and use a shell without customizing it. Shells like ZSH shine once they have individual configurations: configurations that make you - the user - super productive. I wrote this article for inspiration. It should demonstrate how you can increase your overall productivity with simple things like adopting the 5 types of aliases in ZSH. Aliases are the first step towards a tailored shell experience. Aliases are a super individual thing.

I use Docker, Kubernetes, and Microsoft Azure every day. That said, it makes sense for me to have aliases supporting me with these tools and environments. However, maybe you are using different clouds and command-line tools so that you will end up with different aliases. The key takeaway should be that you create and use aliases to help you get your job done.

All your aliases are defined in ~/.zshrc . ZSH loads the configuration file during startup. However, you can always force to reload your configuration file use source ~/.zshrc .

This article will teach you how to create and use these four types of aliases:

Simple Aliases

Simple Aliases are a good starting point. I use them to reduce the number of keystrokes to fire up a command. The definition of a simple alias uses the alias my-alias="command" pattern.

# navigation aliasesalias dev="cd ~/dev/"alias personal="cd ~/dev/thorstenhans"alias business="cd ~/dev/thinktecture"# kubectl aliasesalias k="kubectl"alias kgx="kubectl config get-contexts"# docker aliasesalias d="docker"alias dps="docker ps"

Having those aliases registered in my ~/.zshrc , I can navigate quickly to commonly used directories and to fire-up commands that I use frequently using just a few keystrokes.

Suffix Aliases

Suffix Aliases are defined using the -s flag. With suffix aliases, you can launch files with a specific extension (or suffix) in your favorite tool. To register a suffix alias, we use the alias -s extension=name-of-the-tool pattern. The following samples assume that VisualStudio Code is installed and can be launched using the code command from the terminal.

# Azure CLI filesalias -s azcli=code# Markdown filesalias -s md=code# JSON filesalias -s json=code

Load the modified ~/.zshrc and execute the following:

echo "#Hello World" > sample.md# now type the name of the file and commit via ENTERsample.md

The suffix alias translates sample.md into code sample.md , and you should see VisualStudio Code launching and showing the content of sample.md .

Functions for Aliases With Parameters

Sometimes you want to create aliases the require some contextual information. Parameters are used to describe this contextual information for shell commands. The registration of aliases with parameters looks pretty similar to a function definition in regular programming languages. We use the following pattern to define aliases with parameters:

aliasname() {  command $firstParam $secondParam}

For example, consider you want to optimize the process of listing Azure Kubernetes Service (AKS) instances in your Azure Subscription. To achieve this, Azure CLI provides the az aks list command. The command allows further configuration by adding several parameters.

The following alias accepts two parameters—first, the Resource Group Name (Resource Groups are logical containers in Azure). The second parameter specifies the desired output format.

getaks() {  az aks list -g $1 -o $2}

We can now use the getaks alias with parameters to look for AKS instances in resource-group-1 and print results in colored JSON ( jsonc ) using getaks resource-group-1 jsonc .

Global Aliases

Global aliases are defined using the -g flag. A global alias is aggressive. Once registered, it replaces all occurrences of the alias name with the specified command. The definition follows the pattern alias -g aliasName="command" .

# I have to ask for Azure Resource Ids frequentlyalias -g qId="--query id -o tsv"

Having qId registered as a global alias, I can optimize my performance when asking for unique resource identifiers using Azure CLI. See the following snippet comparing the regular command, to the optimized one using my global alias:

# proper command to query the identifier of an AKS instanceaz aks show -n myaks2020 -g rg-demo --query id -o tsv# command to query the identifier of an AKS instance with global aliasaz aks show -n myaks2020 -g rg-demo qId

Operating system specific aliases

An operating system specific alias is not a real type. However, I think platform-specific aliases are important—especially when using multiple platforms. I use macOS as a daily driver, so I also have some aliases tied to my rig.

However, I love VisualStudio Codespaces that are using Linux as an operating system. In both situations, I want to use my dotfiles. To achieve this, I use simple if -statements.

# macOS aliassesif [[ $OSTYPE == darwin* ]]; thenalias flush='dscacheutil -flushcache'# Appsalias browse="open -a /Applications/Google\ Chrome.app"# * Browse Azure Portalalias azure="browse https://preview.portal.azure.com"fi

See the following image showing all my platform related aliases for macOS:

Platform specific aliases in ZSH

Use Aliases To Edit and Reload .zshrc

I have two aliases in my ~/.zshrc , which are super-efficient. I open my ~/.zshrc in my favorite editor with ec and source it (apply the current state of ~/.zshrc to your ZSH session) with sc . The alias definition for both looks like this:

# open ~/.zshrc in using the default editor specified in $EDITORalias ec="$EDITOR $HOME/.zshrc"# source ~/.zshrcalias sc="source $HOME/.zshrc"


You have seen four different types of ZSH aliases that will boost your productivity. Foud different types of aliases used to achieve almost everything in your shell.

Take your time. Do some research. Identify the command-line tools you are using daily or regularly, find patterns, and create corresponding aliases that increase your productivity by reducing keystrokes.

Having powerful shells on all operating systems (finally including windows using WSL 2.0), you should care about your shell performance. Make ZSH your own.

I do a lot of conference speaking; if you are presenting at conferences or meetups too, you should start your presentation by quickly explaining the aliases you will use throughout the talk to onboard your audience. Maybe they are using kc instead of k for kubectl . :joy: