Terminal (zsh): Increase Productivity by Creating Aliases & Functions
Tags: tutorial, productivity, zsh
Published: 2020-11-29 17:02:36

*This tutorial will cover how to create aliases and functions in your zprofile file to increase your productivity in the terminal. It assumes that you are on MacOS, using zsh as your shell, and are editing the zprofile file with VS Code. If you are using bash, you can do the same by following this [article](https://medium.com/better-programming/boost-your-productivity-with-aliases-and-functions-88eaf0fa0fb4).*

![https://media.giphy.com/media/g0jlOqo7AJ0FmaHSFZ/giphy.gif](https://media.giphy.com/media/g0jlOqo7AJ0FmaHSFZ/giphy.gif)

**Why use Aliases and Functions?**

As a developer, you'll likely spend a good amount of time in Terminal doing the same things over and over again — things like cd-ing into/ accessing a folder (ex: a "Projects" folder) or running some kind of command (ex: git add, git commit -m, git push, etc). While it may not seem like much these repetitive tasks slowly begin to eat away at your precious time. Making use of aliases and functions can help us solve this "problem".

**How to Access Your zprofie**

Open Terminal and enter:

```bash
code ./.zprofile
```

Once doing so, your VS Code should open your zprofile, where you'll see some ENV variables, aliases, and even functions.

**Aliases**

![https://upload.wikimedia.org/wikipedia/en/thumb/1/1c/Alias-logo.png/250px-Alias-logo.png](https://upload.wikimedia.org/wikipedia/en/thumb/1/1c/Alias-logo.png/250px-Alias-logo.png)

You can think of aliases as a text shortcut for some of your most common commands — like git add, git push, and git commit. 

To create aliases for these three commands specifically, we would add something like this to our zprofile:

```bash
alias ga="git add."
alias gp="git push"
alias gc="git commit"
```

Remember, all we are doing here is creating a text shortcut, so that next time we enter Terminal, instead of typing out "git add .", "git push", or "git commit", we can simply type "ga", "gp", or "gc".

As an additional example, another handy use case for an alias is for when you are deploying a site to Netlify with the [Netlify CLI](https://docs.netlify.com/cli/get-started/). Typically, you would need to type "netlify deploy" or "netlify deploy —prod", but now that we know what aliases are and how to create them, we can simply add the following to our zprofile:

```bash
alias nd="netlify deploy"
alias ndp="netlify deploy --prod"
```

Doing so allows us to simply type "nd" or "ndp" instead of "netlify deploy" or "netlify deploy —prod".

![https://thumbs.dreamstime.com/b/functions-message-word-wooden-desk-cube-blocks-software-development-functions-message-word-wooden-desk-cube-187107835.jpg](https://thumbs.dreamstime.com/b/functions-message-word-wooden-desk-cube-blocks-software-development-functions-message-word-wooden-desk-cube-187107835.jpg)

**Functions**

Functions allow us to perform more involved actions from the terminal — like cd-ing into a folder or opening certain browser tabs. These are just simple examples, but with the ability to [make use of conditionals, for loops, and more](http://zsh.sourceforge.net/Guide/zshguide03.html), you can imagine that this is just the tip of the iceberg in terms of what can be done with functions.

To create a function, we simply need to use the function keyword + whatever we want to call our function + curly braces + the terminal commands you want to run.

Here's how the functions I mentioned above would look within my zprofile.

```bash
function projects {
	cd /Users/$USER/Desktop/Projects
}

function job-search {
  open https://www.linkedin.com
  open https://indeed.com
}
```

My projects function makes use of the USER variable that defaults to my MacOS username. Now when I type "projects" in terminal, it will simply bring me to that folder — no need to type the entire path like I would have had to do in the past. 

For my job-search function, we are making use of *open* which will open a browser tab in my default browser. So in this function, when I type "job-search" in terminal, it will open two browser tabs — one for Linkedin and one for Indeed.

![https://media.giphy.com/media/iEs4yXq5rVlL2/giphy.gif](https://media.giphy.com/media/iEs4yXq5rVlL2/giphy.gif)

After adding your aliases and/or functions in your zprofile — just remember to save the file, then once you open a new Terminal window, you'll be able to make use of them.

Here are some additional resources that may be useful when working with zsh and your zprofile:
[Command Line Productivity with ZSH Aliases](https://blog.lftechnology.com/command-line-productivity-with-zsh-aliases-28b7cebfdff9)
[Moving to zsh (series)](https://scriptingosx.com/2019/06/moving-to-zsh/)
[An Introduction to the Z Shell](http://zsh.sourceforge.net/Intro/intro_toc.html)

Feel free to reach out on any of my socials for questions, feedback, or just to connect / say hello 👋.