Home > Wiki Tips

How to Add or Delete Zsh Alias on Mac? [With Examples]

Updated on Tuesday, May 7, 2024

iBoysoft author Jenny Zeng

Written by

Jenny Zeng
Professional tech editor

Approved by

Jessica Shee

English Français Deutsch やまと Español Português

How to Add/Delete/Use Zsh Alias on Mac? [Examples]

Summary: This post from iBoysoft explains how to add different types of aliases in the Zsh shell on Mac with examples. It also tells you how to delete unwanted aliases in Zsh.

Zsh alias

Using aliases in Zsh has multiple benefits. By creating Zsh aliases, you can simplify lengthy command lines, customize command lines to match your workflow, and reduce the likelihood of typos and errors, greatly improving your command-line experience.

If you use Oh My Zsh, you can execute the command alias to view its built-in aliases. If you don't or want to add aliases to Oh My Zsh, keep reading!

How to add alias in Zsh on Mac?

Zsh aliases are kept in the .zshrc file in your user home directory. However, by default, there's no user-specific zshrc file on your Mac but only a system-wide /etc/zshrc file, unless you have installed On My Zsh or created one yourself.

Regardless, you can take the following steps to create an alias in Zsh.

 Note: Oh My Zsh is a framework for managing Zsh configuration, built on top of Zsh. It inherits all the aliases and configurations set in the base Zsh environment. Therefore, any alias defined in Zsh will also be available in Oh My Zsh.

Zsh has four major aliases: simple/regular aliases, suffix aliases, global aliases, and parametrized aliases. Here's the general process for adding aliases to Zsh.

 Tips: If you're unsure about whether a command name is already in use, you can check it with the command: type command_name or use alias in Terminal to list all aliases.

  1. Open Terminal.
  2. Run the command below to open the zshrc file. (If you don't have a zshrc file, this will create one, name it .zshrc, and store it in your home directory.)nano ~/.zshrcHow to open zshrc file in Terminal
  3. Customize aliases in Zsh with the general syntax.alias flag alias_name='command'How to add alias to Zsh in Terminal on Mac
  4. Keep each alias on a single line if you're defining multiple commands.
  5. After finishing the modification, press Ctrl + O and hit Enter to save changes.
  6. Press Ctrl + X to exit nano.
  7. Apply the changes to your current shell session. Otherwise, you may encounter the error "zsh: command not found" when using the alias.source ~/.zshrcHow to apply changes to alias in Terminal
  8. Use the Zsh alias on Mac freely.

Detailed examples are given below to help you understand how to use the general syntax for different types of Zsh aliases.

 Warning: Some users may find a Zsh alias not working, no matter how many times they try. This is usually due to mistakes on the alias, for example, extra space or the lack of a closing quotation mark.

Simple aliases in Zsh

Simple aliases are the most commonly used type of alias in Zsh. They don't have a flag and are only expanded if they are the first word of a simple command.  They are typically used to create shortcuts for frequently used commands.

Examples of simple aliases:

Clear Screen:

alias cls='clear'

This alias expands cls to clear, allowing you to clear the terminal screen with a shorter command.

Git Status:

alias gs='git status'

This alias expands gs to git status, simplifying the command for checking the status of a Git repository.

Show Disk Usage:

alias du='du -h'

This alias expands du to du -h, providing a human-readable disk usage summary.

Interactive File Removal:

alias rm='rm -i'

This alias expands rm to rm -i, prompting for confirmation before removing files.

Global aliases in Zsh

Global aliases, on the other hand, are defined using the alias -g command. They are expanded anywhere in the line where they appear. Global aliases are useful for creating shortcuts for parts of commands or for inserting text at various positions within a command.

Examples of global aliases:

Directory Listing:

alias -g L='| less'

This alias expands L to | less, allowing easy scrolling and viewing of command output.

Pattern Matching:

alias -g G='| grep'

This alias expands G to | grep, facilitating quick searching for specific patterns in command output.

Head Display:

alias -g H='| head'

This alias expands H to | head, enabling the display of the first few lines of command output.

Case-Insensitive Less:

alias -g L.='| less -i'

This alias expands L. to | less -i, providing case-insensitive search functionality in the "less" command.

Line Count:

alias -g C='| wc -l'

This alias expands C to | wc -l, allowing the counting of the number of lines in command output.

Zsh alias with parameter/arguments

Parametrized aliases, also known as "aliases with arguments," are a feature in the Zsh shell that allows you to create aliases that accept parameters or arguments. These parameters can be used within the alias to customize its behavior based on the specific input provided. Here are five examples of parametrized aliases:

Simple File Search:

alias -s g='grep -r'

This alias allows you to search for a specific pattern in files by providing the search term as a parameter, like this: g pattern_to_search.

Quick Directory Change:

alias -s cd.='cd ~/projects/'

This alias allows you to quickly change to a specific directory by providing the directory name as a parameter, like this: cd. project_name.

Customized Grep:

alias -s mygrep='grep --color=auto'

This alias allows you to use a customized grep command by providing additional options as parameters, like this: mygrep -i search_term file.txt.

File Count:

alias -s countlines='wc -l'

This alias allows you to count the number of lines in a file by providing the file name as a parameter, like this: countlines file.txt.

Quick Git Commit:

alias -s gc='git commit -m'

This alias allows you to quickly make a git commit with a specified message by providing the commit message as a parameter, like this: gc "commit message".

Suffix aliases in Zsh

Suffix aliases are a feature in the Zsh shell that allows you to define aliases for specific file extensions. When you use a command with a file that has a specific extension, the alias will be automatically applied to that command. Here are five examples of suffix aliases:

Markdown Preview:

alias -s md=typora

This alias allows you to open Markdown files with Typora for preview by simply typing the file name, like this: example.md.

PDF Viewing:

alias -s pdf=evince

This alias allows you to open PDF files with Evince for viewing by simply typing the file name, like this: document.pdf.

Image Viewing:

alias -s jpg=feh

This alias allows you to open JPEG files with Feh for viewing by simply typing the file name, like this: photo.jpg.

Text Editing:

alias -s txt=nano

This alias allows you to edit text files with Nano by simply typing the file name, like this: notes.txt.

Python Script Execution:

alias -s py=python3

This alias allows you to execute Python scripts with Python 3 by simply typing the file name, like this: script.py.

Share this post to help others set aliases in Zsh.


How to delete alias in Zsh on Mac?

Sometimes, an alias may not work as expected or be no longer required. In such cases, you can delete the alias in the Zsh shell with the following command:

unalias alias_name

For example, to delete the alias cls, which expands to clear, you can use the command: unalias cls.

Please share this guide if you find it helpful!