Create your own terminal shortcuts on your Mac.
By Gemma Lara Savill
Published at October 26, 2023
How to create your own custom shortcuts for your most used terminal commands on your Mac.
This is something I knew existed, as I have watched colleagues use shortcuts for Git, Kubernetes, etc... and I thought "Oh, that looks handy" but I never got down to setting up my own terminal shortcuts.
Now I'm really pleased that I have. And I keep finding new shortcuts to add.
Initially, I thought it might be complicated and that I'd need to set aside some time to configure my custom shortcuts. This is not so, two commands and you are all setup.
I also thought that I might forget commands if I only used my custom shortcuts, but I can open the file that contains my shortcut map with one easy command and remind myself of the whole long terminal instruction.
And a bonus, you can concatenate commands and use parameters.
Sounds good? Let's start.
-
Open your bash config file. The one where you might have your Java path and other custom variables.
Typing
echo $SHELLin your Terminal tells you which shell you’re using.echo $SHELLIf you’re using
ZSH(Z shell) the configuration file you are after is~/.zshrc.
If you’re using Bash, it will be~/.bash_profileor~/.bashrc.
If you’re using a different shell, the file path and filename will be different on your machine.I use VisualCode to edit my files, but you can use Vi or any other program of your choice.
So you can usecode ~/.zshrcor
vi ~/.zshrc -
In this file write your custom terminal shortcuts. Whatever you need. Git ones, Docker ones, long commands that you use often.
For example, in a project I am working on, when I need to create a new branch for a feature or bug fix, I need to check out develop, make sure I pull and recourse submodules and then create my new branch. With my new shortcut alias, all I need to type is:
gs new-branch-nameThe alias for all this is:
alias gs="git checkout develop && git pull --recurse-submodules && git checkout -b $1"So much faster!
-
When you have finished adding your shortcut aliases to the configuration file, you will need to save it with this terminal command:
source ~/.zshrcAnd if you want to be extra safe, open and close the terminal window.
There you go. That is all. Now when you write a long command you often use, just create your shortcut alias.
My latest one is:
alias wip="git stash push -m $1"
wip alias-post
Now I can easily stash away my changes with a keyword!
Who knows what shortcuts tomorrow might bring.