---
title: "Creating a ZSH Alias to Boost Your Shell Productivity"
description: "Discover the many uses of a ZSH alias and take your shell productivity and scripting to the next level with this in-depth tutorial by ATA Learning!"
canonical: "https://adamtheautomator.com/zsh-alias/"
---

# Creating a ZSH Alias to Boost Your Shell Productivity

> Discover the many uses of a ZSH alias and take your shell productivity and scripting to the next level with this in-depth tutorial by ATA Learning!

Source: https://adamtheautomator.com/zsh-alias/

---

ATA Learning

Tap to hide

[

ATA Learning

](/)

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Search for:  

*   [](https://twitter.com/adbertram)
*   [](https://github.com/Adam-the-Automator)
*   [](https://www.linkedin.com/company/adam-the-automator-llc)
*   [](/feed/)

![Creating a ZSH Alias to Boost Your Shell Productivity](https://adamtheautomator.com/wp-content/uploads/2022/11/Creating-a-ZSH-Alias-to-Boost-Your-Shell-Productivity.jpg)

# Creating a ZSH Alias to Boost Your Shell Productivity

[![](https://secure.gravatar.com/avatar/2788bb1a3f735603f81eca51d68daec56a9d97e805a10268fb2c20afcc76b81b?s=192&d=mm&r=g)Nicholas Xuan Nguyen](https://adamtheautomator.com/author/nicholas-xuan-nguyen/)25 November 20226 min. read

Categories: [IT Ops](/category/it-ops/)

Tags:[Linux Shell Scripting](/tag/linux-shell-scripting/)[Shell](/tag/shell/)

Table of Contents

*   [Prerequisite](#prerequisite)
*   [Creating Simple ZSH Aliases](#creating-simple-zsh-aliases)
*   [Making Aliases Permanent](#making-aliases-permanent)
*   [Creating ZSH Suffix Aliases](#creating-zsh-suffix-aliases)
*   [Creating a ZSH Global Alias](#creating-a-zsh-global-alias)
*   [Creating Function ZSH Aliases](#creating-function-zsh-aliases)
*   [Unsetting or Deleting Aliases](#unsetting-or-deleting-aliases)
*   [Conclusion](#conclusion)

Productivity is the key to success. Shaving off even a few seconds here and there can make a big difference in overall efficiency in the fast-paced world.

This tutorial will show you how to create aliases in ZSH that can boost your productivity by automatically expanding specific commands. By the end of this tutorial, you will learn to create and use aliases to save time and keystrokes.

Read on and increase productivity with ZSH aliases!

## Prerequisite

This tutorial houses hands-on demonstrations. To follow along, you will need a Linux server running [ZSH](https://github.com/ohmyzsh/ohmyzsh/wiki/Installing-ZSH). This tutorial uses Ubuntu 20.04, but any modern Linux distribution should work.

Related:[How to Install Ubuntu 20.04 \[Step-by-Step\]](https://adamtheautomator.com/install-ubuntu/)

## Creating Simple ZSH Aliases

When you feel tired of manually typing the same commands many times over, creating an alias will do you a favor. An alias is a user-defined shortcut for a command or set of commands you can invoke like any other command.

The basic syntax for defining an alias is as follows: `name` is the alias’s name, and `‘command’` is the command the alias represents.

```bash
alias name='command'
```

The simplest type of alias consists of a single word, like the `ls` command, which lists the contents of a directory. But you can create an alias for the `ls` command to include options by default.

Related:[Linux Directory Commands : A Complete Guide](https://adamtheautomator.com/linux-directory-commands/#Listing_Files_and_Folders_With_the_ls_Command)

To see how a simple ZSH alias works:

1\. Run the `ls` command below to list all contents of the working directory.

```bash
ls
```

![Listing all files and directories in the working directory](https://adamtheautomator.com/wp-content/uploads/2022/11/image-335.png)

Listing all files and directories in the working directory

2\. Next, run the below command, which does not provide output but creates an alias called `ls` that runs the `ls -la` command when invoked.

> 💡 _Just so you know, creating aliases on the terminal does not provide output throughout this tutorial._

This short alias saves you time from having to type the full `ls -la` command whenever you list the contents of a directory. `alias ls='ls -la'`

```bash
alias ls='ls -la'
```

> 💡 _When you set an alias, ensure it is long enough to be unique (at least two characters or more). For example, writing `ls` instead of just `l` is a good idea. Doing so prevents accidentally expanding the alias as you type out the letter l._

3\. Now, run the `ls` alias without options to list all contents in the working directory, including the hidden ones.

```bash
ls
```

Notice below that you get more contents in the output since the `ls` alias represents the `ls -la` command.

![Testing the ls alias](https://adamtheautomator.com/wp-content/uploads/2022/11/image-336.png)

Testing the `ls` alias

## Making Aliases Permanent

You have seen how the magic of running commands by only invoking a short alias works, and that is a good start. But remember, creating an alias right on the terminal is temporary and only works on the current shell session.

To make an alias permanent, add the alias to your _~/.zshrc_ file, which is read each time a new shell is started. As a result, any aliases defined in that file will be available in each new shell.

1\. Open the _~/.zshrc_ file in your preferred text editor.

2\. Next, add your alias to the end of the file, save the changes and close the editor.

```bash
alias ls='ls -la'
```

3\. Run the below [`source`](https://linuxhint.com/bash_source_example/) command, which does not provide output, but sources the _`~/.zshrc`_ file to make the alias available in your current shell. `source ~/.zshrc`

```bash
source ~/.zshrc
```

4\. Now, close your current shell session and open a new one.

5\. Lastly, run the `alias` command to list all currently defined aliases in your shell. `alias`

```bash
alias
```

As you can see below, the `ls` alias you created is listed in the output. At this point, as you open a current shell, running the `ls` alias interprets the `ls -la` command.

![Listing all currently defined aliases](https://adamtheautomator.com/wp-content/uploads/2022/11/image-337.png)

Listing all currently defined aliases

## Creating ZSH Suffix Aliases

If creating an alias for a command works, can you create one for a default program? Yes! A suffix alias lets you register a specific extension with one particular tool.

For example, you can use a suffix alias to tell ZSH to open all _.txt_ files with the Neovim (Nvim) text editor instead of the default one (nano or vim).

Related:[How to Install Neovim (NVIM) on Windows](https://adamtheautomator.com/nvim/)

The syntax for defining a suffix alias is as follows where:

*   `extension` is the file extension to register.
*   `program` is the program’s name where a file opens with the registered extension.

```bash
alias -s extension=program
```

> 💡 _From this point to the end of the tutorial, you can run the commands/code on the terminal or put them in the ~/.zshrc file to make them permanent._

Run the following command to create a suffix `alias` called `txt`. This alias sets `nvim` as the default program for opening _.txt_ files.

```bash
alias -s txt=nvim
```

Now, type the name of the text file and press Enter to open the file. This example opens the `*zsh-alias.txt` file

```bash
zsh-alias.txt
```

Notice that you did not have to specify which text editor to open the text file since your alias tells ZSH that Nvim is the default text editor.

![Opening a text file](https://adamtheautomator.com/wp-content/uploads/2022/11/image-338.png)

Opening a text file

The _zsh-alias.txt_ file opens in Nvim instead of the default text editor, as shown below.

![Verifying a text file opens in Nvim by default](https://adamtheautomator.com/wp-content/uploads/2022/11/image-339.png)

Verifying a text file opens in Nvim by default

## Creating a ZSH Global Alias

The aliases you have created are only available for the current user. But if you wish to make your aliases available to other users, you must create ZSH global aliases. A global alias is just like a user alias but is available to all users on a system.

Perhaps you want all users on your system to use the same text editor. You can set a global alias for the text editor program in this case.

The syntax for defining a global alias is as follows where `global_alias` is the name of the alias and `path/to/command` is the command you want to run when the alias is invoked.

```bash
alias -g global_alias='path/to/command'
```

Run the below command to create a global alias called `NANO`, which sets Nvim (_`/usr/bin/nvim`_) as the default text editor for all users on your system.

> 💡 _The best practice in creating global aliases is writing the alias name in all caps. Doing so makes the global aliases stand out from other aliases, especially when viewing the list of aliases._

```bash
alias -g NANO=/usr/bin/nvim
```

![Creating a ZSH Global Alias](https://adamtheautomator.com/wp-content/uploads/2022/11/image-340.png)

Creating a ZSH Global Alias

> 💡 _Note that a global alias is aggressive, as it overwrites an alias with the same name you already specified._

Now switch to a different user, and run the `NANO` alias to open a text file (`*zsh-alias-global.txt*`).

```bash
NANO zsh-alias-global.txt
```

You can see below that the text file opens in Nvim regardless of the user logged in.

![Testing a ZSH global alias](https://adamtheautomator.com/wp-content/uploads/2022/11/image-341.png)

Testing a ZSH global alias

## Creating Function ZSH Aliases

When dealing with a complex set of commands, more than a simple ZSH alias is required. But worry not! Instead, create a function ZSH alias that points to a specific shell function.

The syntax for defining a function alias is as follows.

```bash
alias name-of-alias="command1 commands2 ... "
```

Run the code below, which does not provide output, but defines a ZSH function alias (`cmp`), which compares two numbers. This code uses “[if-else](https://adamtheautomator.com/bash-if-else/)” statements to compare the two numbers and print the result.

Related:[Understanding Bash If Else and other Conditional Statements](https://adamtheautomator.com/bash-if-else/)

```bash
cmp() {

  if  [[ "$1" -gt "$2" ]]; then

     echo "$1 is greater than $2"

  elif [[ "$1" -eq "$2" ]]; then

     echo "$1 is equal to $2"

  else

     echo "$1 is less than $2"

  fi

}
```

Now, invoke the compare function (`cmp`) alias to compare two numbers (`10` and `20`).

```bash
cmp 10 20
```

Below, the `cmp` function alias compares the numbers and prints a message result accordingly.

![Testing the cmp function alias](https://adamtheautomator.com/wp-content/uploads/2022/11/image-342.png)

Testing the `cmp` function alias

## Unsetting or Deleting Aliases

Perhaps you no longer need an alias previously set by you or another user. If so, you can unset or delete that alias, avoiding confusion about which alias does which.

You can use the `unalias` command to remove aliases in ZSH. The syntax for the unalias command is as follows. Replace `name` with the name of the alias you want to remove.

To unset or delete an alias:

Run the following command to list all currently defined aliases.

```bash
alias
```

Note the name of the alias to delete from the list.

![Listing all currently defined aliases](https://s3-us-west-2.amazonaws.com/secure.notion-static.com/5ab566e4-cf27-4632-b9b4-645b8e5e834e/list-of-aliases2y.png)

Listing all currently defined aliases

![Listing all currently defined aliases](https://adamtheautomator.com/wp-content/uploads/2022/11/image-343.png)

Listing all currently defined aliases

Next, run the `unalias` command to delete your target alias, in this case, `ls`. This command does not provide output, but you will later verify the deletion went through.

```bash
unalias ls
```

Finally, run the `ls` command below to list all contents of the working directory.

```bash
ls
```

This time, you will see that output is much shorter since running the `ls` command no longer include the `-la` options by default.

![Unsetting or deleting aliases](https://adamtheautomator.com/wp-content/uploads/2022/11/image-344.png)

Unsetting or deleting aliases

## Conclusion

Productive ZSH users heavily use aliases to minimize the amount of typing just to run the same commands. The good news is that, in this tutorial, you have learned how to create a ZSH alias in different types and set global aliases (system-wide).

Now, why not use ZSH aliases in your scripts to automate complex or repetitive tasks?

Share this article

[Share on X](https://twitter.com/intent/tweet?url=https%3A%2F%2Fadamtheautomator.com%2Fzsh-alias%2F&text=Creating%20a%20ZSH%20Alias%20to%20Boost%20Your%20Shell%20Productivity)[Share on Facebook](https://www.facebook.com/sharer/sharer.php?u=https%3A%2F%2Fadamtheautomator.com%2Fzsh-alias%2F)[Share on LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fadamtheautomator.com%2Fzsh-alias%2F)

## Related Posts

![](https://adamtheautomator.com/wp-content/uploads/2024/03/pipe-command-in-linux-1.jpg)

### [Unleashing the Power of the Pipe Command in Linux](/pipe-command-in-linux/)

Unlock the prowess of the pipe command in Linux to streamline tasks, boost productivity, and simplify complex operations effortlessly!

![](https://adamtheautomator.com/wp-content/uploads/2022/08/Learning-Bash-File-Test-Operators-With-Examples.jpg)

### [Learning Bash File Test Operators With Examples](/bash-file-test/)

Learn how to use a Bash file test to make sure that your code and scripts execute the way you need, every time!

![](https://adamtheautomator.com/wp-content/uploads/2022/07/How-to-Update-Ubuntu-IP-and-Hostname-via-Bash.jpg)

### [How to Update Ubuntu IP and Hostname via Bash](/change-linux-ip-hostname-bash-script/)

Discover and learn how to change a Linux IP and Hostname via a Bash script in this new tutorial by ATA Learning!

## Categories

*   [IT Ops](/category/it-ops/)
*   [Cloud](/category/cloud/)
*   [DevOps](/category/devops/)
*   [Home Ops](/category/home-ops/)
*   [Information Security](/category/infosec/)
*   [Software Development](/category/software-development/)

## Site

*   [Home](/)
*   [Tutorials](/tutorials/)
*   [Instructors](/author/)
*   [Advertising](/advertising/)
*   [Recommended Resources](/resources/)
*   [About Adam](/about-adam/)

Copyright 2026© ATA Learning | [Privacy Policy](/privacy/)
