DEV Community

Cover image for Custom Alias / Shortcut in mac Terminal
Leah Vogel
Leah Vogel

Posted on

Custom Alias / Shortcut in mac Terminal

TL;DR:
$ open ~/.zshrc
$ alias yoursmartaliasname='your smart command' #no spaces before or after the equal sign
# save your text file
# restart terminal to use your new alias

Image description


Many moons ago I was irritated by the fact that I had a lot of local branches of work on my computer, after those branches had already been pushed, reviewed and merged into the remote repository. I wanted to delete them locally as well.
I found a cool command to clean them up, but there was no way I would be able to remember it when needed. I put it on a digital sticky note (at the time, a text file, easily accessible for the good old copy-paste) and I copied it from there whenever I needed it.

Here’s the command — just in case you need it too:
git branch — merged | egrep -v “(^\*|main|develop)” | xargs git branch -d


I know, I know — pasting from a text file isn’t the most efficient way of doing things.

I thought to myself, there must be a better way… and of course there is!

Create a custom alias (or shortcut) to be used in the command line for easy usage.

So let’s learn how to define an alias! Important to note: in order for the alias to be permanent, you need to save it in your terminal’s config file.


Who is your terminal?

To find out which type of shell your terminal is using, type in the following command:

$ echo SHELL #all caps


Open your config file:

Based on the output you then open your config file, as follows:

Image description

This will open the config file for editing, using your default editor.

⚠️ Warning to the wise, make sure you know how to save and exit your text editor 😃

Image description

Set up the alias:

You will then need to type the alias, making sure to have a quote around your command, and no spaces before or after the equal sign:

alias prune='git branch — merged | egrep -v “(^\*|main|develop)” | xargs git branch -d'

I decided to call my alias prune, because it’s similar to the git prune command, because in my head it made sense (which is the only thing that matters because it’s a custom alias for my own computer 🙃).


Save and Restart Terminal

Next, save your config file and restart your terminal for the alias to be used.

Now, running that command looks like this for me:

Image description

That’s it! Happy aliasing 🧑🏻‍💻🤩


originally posted at https://medium.com/@vogeleah/custom-alias-shortcut-in-mac-terminal-26a6b688d133

Top comments (0)