As a developer, I spend a fair amount of time in the terminal. There are commands that I use over and over again that I've decided to set up shortcuts for to make my life easier.
Need to revert your last commit? Normally that's git reset --soft HEAD~1. This isn't a difficult command to remember, but I prefer to type out git undo. The way I set this up is with my git configuration. I simply typed the following into my terminal:
If you want to set up aliases for a specific repository use the --local option instead of --global. The default is --local if you don't pass in any option. Other aliases I set up just because I want to type less or I frequently mistype are:
If you want to see all the git aliases you've set up:
$ git config --global -l
Since 2019, the default macOS shell switched from bash to zsh. If you use bash, you will want to find a different guide. To set up aliases in zsh you need to edit the .zshrc file in your home directory. I use VSCode as my editor of choice, so to view and edit this file I type the following into my terminal:
$ code ~/.zshrc
Other more general commands you might use to open your .zshrc file from the terminal are:
$ open ~/.zshrc
$ open -a TextEdit ~/.zshrc
You can replace TextEdit in the second command above with the name of your preferred application if you don't already have a command set up. Once you have this file open, be very careful with editing! This script runs whenever you start up the shell. If something is misconfigured it can mess up your ability to use the terminal. At the bottom of this file, start a section for aliases and use the following syntax to set them up:
# aliases
alias your-custom-alias="some command"
Some of my aliases are commands that I frequently mistype.
# aliases
alias gti="git"
Other aliases are long commands that I want to shorten. For example, if there is a repo that I frequently work on and want a quick command to get there from anywhere, I set up an alias.
# aliases
alias repo-name="cd /Path/to/my/repo"
I've also set up aliases to run tests that have a lot of options set up in a specific way. You can also set up parameterized aliases that take arguments for more complicated commands. You can have multiple params or use a single param multiple times. I haven't found a reason to set one up myself yet but an example of the syntax to do so is below.
# parameterized alias syntax:
parameterized-alias-name() {
command $1 ... $2
}
# example:
foo() {
mkdir $1 && cd $1
}
The example above can be used as: $ foo new-directory to create a new folder and immediately move into that folder in one command.
After you are done setting up your aliases in your .zshrc file, save your changes. Every new terminal tab or window will have everything properly loaded, but any sessions that you already have open will need to reload the .zshrc file for your new aliases to work. You can reload the config with:
$ source ~/.zshrc
Find a typo or broken link? Something not clear? Submit an issue to my github repo!