Aliases: simplify your command-line life

Automate, auto-correct, type less, don’t repeat yourself…

I’ve been a programmer for the last 10 years and, most (or at least a significant amount) of this time, I spent typing on terminal.

git add —all
docker rm -f $(docker ps -qa)
npm run test
cd tests/unit-tests && dotnet run test && cd ../../

Are some examples of commands that are often repeated over and over again. Add to that that I’m a twrrible [sic] typer and because of that I have to retype the same commands 2 or 3 times until I get it right and I can be a little lazy to read and remember the docs for specific commands and the command line experience starts to become tedious and frustrating.

Enter alias. This is not a new concept and I had used before for mapping environment variables (who never had to map JAVA_PATH, ANDROID_HOME?) but I had never realized that I could use those for my own benefit to help me type less and to “auto-correct” my typos.

Take for example the git add command. I frequently typed:

gti add --all

and gti is not a valid command, or if it is, it's not exactly what I wanted to do. So, I had to write that same line again to correct the misspell.

With an alias, I can easily workaround that. By mapping gti to git, now anytime I write the wrong line, the terminal replaces that by the correct command and I don’t need to retype it 🙂. Furthermore, because that same line will be repeated multiple times a day, why not replace the whole line with something simpler? So now I have gita and gtia mapped to execute that line. With a simple

alias gti=“git”
alias gita=“git add —all”
alias gtia=“gita”

in my terminal startup, I now solved two problems: 1. mistyping and 2. less code to memorize.

How to do it?

It’s easy to start using aliases and every shell has a way to do it.

If you use bash, edit ~/.bashrc

If you use zsh, edit ~/.zshrc

(I promise I’m gonna add Powershell, for the Windows users, later).

And at the end of the file, simply add the aliases that you want, save the file and restart the shell.

Here are some examples of aliases that I use:

# Helpers
alias dcup="docker-compose down; docker-compose build; docker-compose up --remove-orphans" #rebuild and run a docker container
alias dnrb="rm -rf ./bin && rm -rf ./obj && dotnet nuget locals all -c && dotnet restore && dotnet build" # removes all nuget caches and rebuilds a dotnet application
alias brew-upgrade="brew update && brew upgrade && brew upgrade --cask --greedy && brew cleanup" #upgrade all packages installed using homebrew

# Git Shortcuts
alias gita="git add --all && git commit -n -m"
alias gitps="git push"
alias gits="git status"
alias gitcm="git checkout master"
alias gitc-="git checkout -"
alias gitrh="git reset --hard"
alias gitpl="git pull"
alias gitmm="git merge master"

# Mispelling
alias celar="clear"
alias cleqar="clear"
alias cpde="code"
alias exti="exit"
alias exot="exit"
alias code.='code .'
alias got="git"
alias gtis="gits"
alias ckear="clear"
alias cd-="cd -"
alias gticm="gitcm"
alias clea="clear"

These are just some examples of what can be done. You can also implement functions and more complex automations. With aliases, the sky is the limit and once you start using, you can never go back.