How to Make Alias on Mac with Terminal

We often use computers to be more productive and get things done faster. It’s what they are designed to do, and if you use the Terminal application on Mac, you can learn many functions such as creating an alias, to reduce keystrokes and increase productivity.

To make an alias on your Mac with Terminal, use the “alias” command in a current Terminal session or set it up so that it is available anytime you start the Terminal app. Making aliases for commands you use frequently, or ones with many parameters can help you boost your efficiency to new levels.

My name is Eric, and I love using a computer’s capabilities to make life easier. I am a frequent user of command line applications like Terminal, and I often create aliases for the commands I use most often and for hard-to-remember commands, so I don’t have to spend time looking them up.

Keep reading below if you would like to see how you can create alias commands to help you use your Terminal application more effectively.

What is an Alias

Before we review how to make an alias on Mac with Terminal, it’s important first to know what an alias is. 

An alias, in essence, is a shortcut you can use in a command line environment such as the Terminal application on Mac. It is a string of characters you define and use to call another command or set of commands which may or may not include command parameters.

Aliases are often used when we have a long command that is hard to type, and we would like to shorten it. For example, I often use the ls -ltra to list the contents of a directory in detail. Using an alias, I could shorten this command so that I only need to type the letter l.

While this simple command is already relatively short, you may have much longer commands that can be much more difficult to type. Reducing them to an easy-to-remember command with only a few letters can save a great deal of time in the long run.

Using the Alias Command to Create an Alias

Creating an alias on Mac with Terminal is a simple one-line command, and if you are just looking to create one for your current Terminal session, all you have to do is enter the command below into a terminal session.

alias <string>=<command>

Where <string> is any string of characters you want to use in place of the command, and <command> is the command you would like to execute when you type the string.

Using my example above, the command would be:

alias l=’ls -ltra’

Now you only need to type the l key to do a ls -ltra command. Remember that this will only be in effect during your current Terminal session. Once you close the session, the alias you defined will be gone. 

If you want to ensure that the alias is available every time you open Terminal without needing to type it in, you can enter it into your shell profile. I will show you how to do that in the next section.

Suppose you need to see a list of the aliases you currently have set up in your session. In that case, just type the “alias” command by itself without the <string>=<command>, and it will show you the list of them.

Add the Alias to your Shell Profile

If you would like to set up Terminal so that your aliases are always available every time you start it up, you can do this by adding the command to your shell profile. 

Follow the steps below to do this.

Step 1: Start Mac Terminal.

Go to LaunchPad and search for Terminal. Once you see it, click on it to start it up.

Step 2: Make sure you are in your home directory.

Type cd and hit the return/enter key. This will ensure you are in your home directory.

Step 3: Determine which shell you are using.

You can find out which shell you are running by typing in the command below.

echo $SHELL

For most newer systems, you will probably be using zshell, so you will see a zsh, but if you are using bash shell, you will see bash. For our example below, we will use zsh.

Step 4: Open the shell profile file.

For bash shell, the file would be .bash_profie, and for zshell (as in our example), the file is .zprofile. Since these files are hidden, you will have to use the -a parameter if you want to see them in the directory with the ls command. 

Use the nano command to open the file in the nano editor.

nano .zprofile

Step 5: Add the alias commands to the file.

Once you open the file, use the cursor arrows to move to the bottom, then add your alias command to the end of the file. 

Don’t forget to save the file after you add the alias command.

Step 6: Exit Terminal and restart it.

Use the CONTROL+X keys to exit the nano editor, then exit the Terminal altogether. When you restart Terminal, your alias command will take effect.

FAQ

Below are some common questions about creating an alias with Terminal on Mac.

Why use aliases?

If you use Terminal frequently, aliases will make your life easier by shortening long and complicated commands. It will boost your productivity and efficiency. 

Are there any drawbacks to using aliases?

The only problem with using aliases is that you may get into the habit of just using the alias and forget how to use the original command.

Conclusion

Using aliases can help you be more productive when working in Terminal but be careful not to rely on them. You want to make sure that you still learn and remember basic commands, so you can still function without using aliases when/if necessary.

Please let me know if you have any questions or comments!

Leave a Reply

Your email address will not be published. Required fields are marked *