How to Create an Alias for Mac with Terminal (Bash or Zsh Shell)

You are currently viewing How to Create an Alias for Mac with Terminal (Bash or Zsh Shell)

Aliases are handy to make the workflow more efficient. It shortens the process of retyping repetitive commands in your terminal.

One of the scenarios we use here on our WordPress website is when running promotions.

When running a sale on our website, we need to edit four files: style.css, functions.php, our Plugin Manager server file, and a specific JS file.

Up until the point I created the alias, I needed to navigate into each of the file folders and open them individually.

The command that I used to open them all was:

open store/wp-content/plugins/pluginsforwp-server/pluginsforwp-server.php store/wp-content/themes/oceanwp-child-theme-master/functions.php store/wp-content/themes/oceanwp-child-theme-master/scripts/general-scripts.js store/wp-content/themes/oceanwp-child-theme-master/style.css

A process like so will waste valuable time. Good developers should aim to be more productive and simplify operations. Therefore, creating an alias is precisely what I needed to do.

Thus, I created the alias promofiles to open all four files.

In the next section, I will show you how to set an alias in Unix environments, such as macOS, Bash, Zsh, and Linux.

Temporary Alias

The first type is a temporary alias, valid only for the current session (until closing the shell) or turning off your computer.

First, you’ll need to open the terminal. Then, use the syntax below:

alias <newcommand>='<old command>’

For example, if we would like to use the command pull to pull our WordPress website from git, we can create an alias like so:

alias pull=‘git pull origin master’

Once we set it up, we can execute the alias using the pull syntax.

Create temporary alias mac

If you would like to persist with the newly created alias and use it again in the future, we will need to save it permanently.

Permanent Alias

The advantage of a permanent alias is the option to reuse the syntax in the future, even after restarting our system.

To save the alias on our system, we will need to add it to the .bash_profile or .zshrc files.

The following steps will show you how to create an alias for macOS:

  1. Launch the terminal from the Utility folder.

  2. Type ~/ to navigate to your home directory.Terminal Home Directory

  3. Now, you’ll need to open the .bash_profile or the .zshrc file by entering open .zshrc (if you don’t have the file, create one first with the nano .zshrc command and open it afterward).Open zshrc file

  4. Scroll to the bottom of the .zshrc file and enter alias <newcommand>='<old command>’. For example: alias l='ls -lah’.Create Alias for Mac

  5. Save the file and exit it.

  6. Run the alias in the terminal and verify it’s working.

That’s it. You just created an alias on your macOS that will make your developing job much quicker.

Frequently Asked Questions

What environments will the alias work with?

Any UNIX environment: Bash, Zsh, Fish on macOS and Linux.

What is the macOS default shell?

Since the release of macOS 10.6, Zsh has been the default shell (replaced the bash shell).

Can I create an alias in Windows?

Yes, there are many great tutorials that you can find on Google. This is the one we followed in the past and found beneficial.

Related Articles

Conclusion

Alias is a great way to prevent retyping repetitive commands and is very useful in saving valuable time.

Leave us a comment and let us know if you have any questions about the process or need any other command.

Also, make sure to subscribe to our YouTube channel and like our page on Facebook.

PluginsForWP

PluginsForWP is a website specializing in redistributing WordPress plugins and themes with a variety of knowledge about WordPress, internet marketing, and blogging.

Leave a Reply