Create aliases in .bash_profile or .zshrc to assign shortcuts for common Terminal commands

Published

If you’re in Terminal all the time, and you regularly type long commands, you can get back those precious moments of your life by creating aliases for those commands in your ~/.zshrc or ~/.bash_profile, depending on which shell you are using.

Let’s say you often browse to a directory that requires a lot of typing, such as:

$ cd ~/Downloads

You can create a shortcut by adding the following line to your ~/.zshrc or ~/.bash_profile:

alias dl="cd ~/Downloads"

If you don’t know how to edit those files, read my guide that explains how to read and edit dotfiles.

Save the file, then either open a new Terminal tab, or quit and relaunch Terminal, or refresh the changes by typing:

$ source ~/.zshrc

or if you’re using Bash:

$ source ~/.bash_profile

From now on, you can go to your Downloads directory by typing this shortcut:

$ dl

Another useful alias you can create is for opening files with your favorite text editor. In my case, it’s Sublime Text, which already comes with a macOS Command Line tool called subl, but I find that typing subl is awkward. Why not go to the extreme and shorten it to s? So I created the following alias:

alias s='open -a "Sublime Text"'

This allows me to use the following command to open a file with Sublime Text:

$ s ~/.zshrc