One of the three great virtues of a programmer is laziness, and typing the same commands over and over again certainly doesn’t sound like proper laziness!
Thankfully, there’s an easy solution: setting up aliases for frequent commands. Here’s how to do it, in a nutshell.
.bash_profile or .profile
If you’re running Bash in the terminal, the most popular shell on Mac and Linux, you can edit a file in your user’s root folder called .bash_profile.
Here let’s use atom to edit the file:
$ atom ~/.bash_profile
Alternatively, on Mac you may already have a .profile:
$ atom ~/.profile
Edit whichever of .profile or .bash_profile already has content, or if neither have content, then it doesn’t matter which one you edit/create.
alias
Define new aliases with the alias command, and define one per line. Here’s an example of useful aliases for the typical git workflow:
alias diff='git diff'
alias a='git add .'
alias add='git add .'
alias s='git status'
alias status='git status'
alias p='git push'
alias push='git push'
myCommit() {
git commit -m "$1"
}
alias commit=myCommit
alias c=myCommit
Notice how alias commands can’t accept arguments, so we use a function as a workaround to be able to provide our commit message as an argument. With that, we can now use c “commit message” to commit.
Next, save the file, return to the terminal and refresh the Bash environment with the following command:
$ source ~/.bash_profile
Or:
$ source ~/.profile
If you ever forget the aliases that you’ve setup for yourself, simply use the alias command to list the available aliases:
$ alias