Do you ever find yourself typing furiously just to change directories? Typing the same command combination multiple times throughout the day? Good news! Aliases are here to save the day.
What is an Alias?
An alias in the terminal is a command that you can use as a substitute for another command or set of commands.
As developers, we spend all day typing and we can use aliases to make our life easier by typing less.
Let’s explore some examples and learn how to set up Aliases.
I often find myself having to navigate to the Downloads folder so I decided to create an alias of “dwl” to navigate to my downloads folder. With this alias set, no matter which directory I’m currently working in, I can easily navigate to the downloads folder with ‘dwl’. No more pressing tab multiple times to autocomplete directories. Just three letters and it’s done!
Sometimes I want to quickly perform a google search and need to switch to my chrome window. The regular command to open Chrome is long, so I made an alias to open a new chrome window by simply typing “chrome”. If I want to open to a specific page, I can type chrome and the URL after. If you give your alias a URL, it will open in the current window you have open. If you don’t, it will open a new window to www.google.com.
SETTING UP AN ALIAS
By now, I’m sure you’ve realized aliases are an awesome way to save time and do less typing so let’s go through the setup process.
Step1. In order to set up an alias, we must edit our ~/.zshrc file. The zshrc file stores the configuration for your zsh shell. MacOS has migrated the default shell to zsh and you should have a configuration file located at ~/.zshrc. If you don’t, go ahead and create one.
Once we have our file open, we can start the customization! Let’s walk through this process and create a couple of aliases together.
We sometimes need to know our public IP address. We can set up an alias to get our public IP address using the alias below.
As you can see, to create an alias we use the word ‘alias’ followed by the command we want to use to execute and set it equal to our regular terminal command.
When you spend a lot of time in the terminal, one of the most common things you do is search for files. If you’ve worked with Linux, you know you can use grep to find files. Unfortunately, the grep in MacOS does not work the same way as in Linux. You can either learn how to operate the grep command in MacOS or install the Linux version of grep. I decided to install the Linux version, but you have to call it with ggrep to avoid conflict with the local grep. To make searching for files easier, I created an alias to search for files below.
This allows me to simply type gf(grep find in my mind) followed by search term and it will return a list of files matching my search term. This is awesome because it saves me so much time!
Aliases and Git
You probably use Git a lot and this is a great place where you can use aliases. But before you create global aliases, you should know that it is recommended to use the Git configuration file for git aliases. This file can be found by typing
git config --list --show-origin . This command will return the location of the configuration file alongside your git configuration and the aliases already included with git. The standard aliases on my configuration file are shown below. If you want to add or edit any, simply follow the examples in the git configuration file. My git configuration file came with all the aliases below.
I hope you love Aliases as much as I do. Think about all of the possibilities and about how your life can be better with Aliases. This is only the beginning and you now know how to simplify your life by creating your own custom aliases in the terminal.
As a final word of advice, make sure your aliases don’t interfere with system commands like cd or pwd because otherwise, neither will work.
Now go explore, create your own aliases, and hopefully do a bit less typing! :)
Resources: If you’d like to get an idea of some cool aliases you can set up on your computer, you can check out the two articles below.
What are your favorite bash aliases? | DigitalOcean
Bash aliases (https://www.digitalocean.com/community/tutorials/an-introduction-to-useful-bash-aliases-and-functions)…