How to Add an Alias in macOS with Terminal

If you are a macOS user, you may find yourself typing the same long and complex commands repeatedly in the terminal. This can be tedious and time-consuming, even if you are using simple commands multiple times. Fortunately, macOS allows you to create aliases, which are custom keyboard shortcuts for commands that you use frequently. Here is how to make an alias in the macOS Terminal in five easy steps.

Creating an alias in the terminal will depend on your shell. Prior to 2019, Macs shipped with bash as the default shell. Starting with macOS 10.6 Catalina, the default shell is zsh.

Step 1: Open the Terminal App

To begin, open the Terminal app on your Mac. You can do this by clicking on the Launchpad icon in your Dock and searching for “Terminal” or by pressing Command + Space and typing “Terminal” in Spotlight.

Step 2: Open .bash_profile or .zshrc

Next, open your configuration file for your shell environment. For bash, you’ll be editing .bash_profile, and for zsh you will edit .zshrc. These files allow you to customize the Terminal settings and add aliases. To start editing, type one of the following command in the Terminal:

nano ~/.bash_profile

or

nano ~/.zshrc

Step 3: Add Your Alias

The syntax for creating an alias in the Terminal is as follows:

alias shorcut = 'command'

Replace “shortcut” with the custom name for your alias and “command” with the actual command you want to create an alias for.

Let’s try an example, and create an alias I use very often. To go up one level in the directory, you would normal type

cd ..

To go up two levels, you would use

cd ../../

To save some typing, let’s create two aliases to make traversing directories faster:

alias .. = 'cd ..'

alias ... = 'cd ../../'

Step 4: Save and Exit Refresh

Once you’ve added your alias, save and exit by pressing Control + X, then Y to confirm that you want to save changes, and Enter to exit.

Step 5: Refresh and Test Your Alias

To test your alias, type the following to refresh the shell environment:

source ~/.bash_rc

or

source ~/.zshrc

Or, you can just quit Terminal and start it up again.

Test out your new alias in the Terminal by typing your custom shortcut. To use our example, try typing .. to go up one level, and … to go up two levels

To add more aliases, simply add more lines to your configuration file using the same syntax.