DEV Community

Cover image for Terminal (zsh): Increase Productivity by Creating Aliases & Functions
Anthony DiPietrantonio
Anthony DiPietrantonio

Posted on

Terminal (zsh): Increase Productivity by Creating Aliases & Functions

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://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:

code ./.zprofile
Enter fullscreen mode Exit fullscreen mode

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

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:

alias ga="git add."
alias gp="git push"
alias gc="git commit"
Enter fullscreen mode Exit fullscreen mode

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. 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:

alias nd="netlify deploy"
alias ndp="netlify deploy --prod"
Enter fullscreen mode Exit fullscreen mode

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

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, 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.

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

function job-search {
  open https://www.linkedin.com
  open https://indeed.com
}
Enter fullscreen mode Exit fullscreen mode

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

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
Moving to zsh (series)
An Introduction to the Z Shell

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

Latest comments (2)

Collapse
 
coquizen profile image
Ian C

Hey Anthony,

Small edit: your projects function could be made simpler with the use of the $HOME environment variable instead.

Collapse
 
antdp425 profile image
Anthony DiPietrantonio

This is true 👀