How to Configure & Use Aliases in ZSH

May 13, 2023 - Leave a Comment

Mac Terminal icon

Command line aliases are very useful, allowing you to have command shortcuts for lengthier commands, and you can use them to replace one command with another as well. With zsh becoming the default shell in Terminal for MacOS, and a widely used shell in general thanks to Oh-My-Zsh, you may be wondering how to configure and use aliases with zsh shell.

How to Create an Alias in zsh

Zsh aliases are stored in the users zshrc file within the home directory, which is prefixed with a period to signify that it is hidden. Thus we must edit that file to configure aliases.

  1. Open the Terminal app if you haven’t already
  2. To edit the .zshrc file in your home directory using nano, use the following command syntax:

    nano ~/.zshrc

  3. Append to the bottom of this text file your desired alias(es) using the following format, with each new alias being on a separate line:
  4. alias (aliasname)="command"

    For example, to make an alias called “fullinstallers” that lists all available complete installers for MacOS using the softwareupdate command with the –list-full-installers flag, the syntax would be:

    alias fullinstallers="softwareupdate --list-full-installers"

    Another example, if you installed gcc with Homebrew on the Mac and you want to make it easier to run gcc rather than clang, you could use the following alias:

    alias gcc="gcc-13"

    Another example, is to use an alias if you find yourself frequently typing in a lengthy command to connect to a particular ssh server, like so:

    alias remoteshell='ssh -p 123123 username@remote.shell.domain.com'

  5. Place each alias onto a new line
  6. When finished modifying your .zshrc file with aliases, hit Control+O to save in nano, followed by Control+X to quit
  7. Back at the command line, use the source command to reload the shell profile configuration:
  8. source ~/.zshrc

Your new alias(es) are now ready to use at the command line, just type the command you linked to the alias, and it will run. Using the examples above, that would be ‘remoteshell’, ‘gcc’, and ‘fullinstallers’.

If you use Oh-My-Zsh on the Mac then you might want to run the ‘alias’ command first because you’ll find many prebundled aliases already in your .zshrc file that you might otherwise be setting up aliases to perform, like using color with ls for example.

.

Related articles:

Posted by: Paul Horowitz in Command Line, Mac OS, Tips & Tricks

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site