Be Super Productive with Shell Aliases

Shahnawaz Ali Kausar
5 min readDec 26, 2020

--

One of the most powerful tools a developer has is Shell/Terminal. I call it a developer’s Grimoire.

Grimoire — Book of Spells

A Shell can be customized to suit your needs and environment. ZSH is among the best shell tool out there, it allows a user to add custom configuration very easily, such as environment and profile support, aliases, functions, plugins, themes, etc.

In this article, we will explore how you can use Aliases to boost your productivity and reduce the number of keystrokes. We will use ZSH, but you can easily use the knowledge here within any Shell supporting Aliases.

Content

  • What is an Alias?
  • How to set Aliases?
  • Useful Aliases
  • Bonus

What is an Alias?

An alias is a shortened version of a command (or a sub-part of it).

Syntax: alias ALIAS_NAME="COMMAND"

Examples:

alias dev="cd ~/folder-a/folder-b/dev"
alias gplo="git pull origin"
  • cd ~/folder-a/folder-b/dev is shortened to dev
  • git pull origin feature/auth is shortened to gplo feature/auth

Amazing! Isn’t it?

How to set Aliases?

By default ZSH loads all aliases from the file ~/.zshrc .

I personally prefer keeping custom aliases in a separate file ~/aliases.sh , it helps me to keep using the same aliases file on different machines.

Step 1: Update file ~/.zshrc — You can use vim or any editor to update it

# ~/.zshrc file
# ...
# some default commands
# ...
# ------------------------
# LOAD CUSTOM ALIASES FROM
# ------------------------
source $HOME/aliases.sh

Step 2: Create a file ~/aliases.sh

# ----------------------
# CUSTOM ALIASES
# ----------------------

alias dev='cd $HOME/dev'
alias learning='cd $HOME/learning
# ...
# We will update this file shortly

Step 3: Reload ZSH configuration in the active ZSH shell.

Run: source ~/.zshrc

Some Useful Aliases

Let’s start adding some useful aliases in ~/aliases.sh

Ease of Access

Adding a few simple aliases for commands you frequently type in the shell would save a tremendous amount of time.

# -----------------------
# Ease of use
# -----------------------
# Navigation
alias dev='cd $HOME/dev'
alias learning='cd $HOME/learning
# Quick Shortened
alias cls='clear'
alias flush-dns='dscacheutil -flushcache'
Usage of `dev`, `learning` and `cls` in terminal

Git Aliases and Functions

Git has made our life easier with its powerful code management features. How about creating aliases for those commands too.

My favorites are: gplod , gploc , gpsoc , gs , gst , gsta and gcm

# ----------------------
# Git Aliases
# ----------------------
alias ga='git add'
alias gaa='git add .'
alias gaaa='git add --all'
alias gau='git add --update'
alias gb='git branch'
alias gbd='git branch --delete '
alias gc='git commit'
alias gcm='git commit --message'
alias gcf='git commit --fixup'
alias gco='git checkout'
alias gcob='git checkout -b'
alias gcom='git checkout master'
alias gcos='git checkout staging'
alias gcod='git checkout dev'
alias gd='git diff'
alias gda='git diff HEAD'
alias gi='git init'
alias gl='git log'
alias glg='git log --graph --oneline --decorate --all'
alias gld='git log --pretty=format:"%h %ad %s" --date=short --all'
alias gm='git merge --no-ff'
alias gma='git merge --abort'
alias gmc='git merge --continue'
alias gpl='git pull'
alias gplom='git pull origin master'
alias gplos='git pull origin staging'
alias gplod='git pull origin dev'
alias gplr='git pull --rebase'
alias gps='git push'
alias gpsom='git push origin master'
alias gpsos='git push origin staging'
alias gpsod='git push origin dev'
alias gr='git rebase'
alias gs='git status'
alias gss='git status --short'
alias gst='git stash'
alias gsta='git stash apply'
alias gstd='git stash drop'
alias gstl='git stash list'
alias gstp='git stash pop'
alias gsts='git stash save'

# ----------------------
# Git Functions
# ----------------------
# Git log find by commit message
function glf() { git log --all --grep="$1"; }
function gploc { git pull origin $(git rev-parse --abbrev-ref HEAD); }
function gpsoc { git push origin $(git rev-parse --abbrev-ref HEAD); }

Note: You might want to checkout GIT plugin that comes with ZSH — as it supports few GIT aliases by default. For this article we will continue using our custom aliases.

Tooling

Keeping some aliases for tools we use on daily basis would be handy too.

# -----------------------
# NPM
# -----------------------
alias nrs='npm run serve'
alias nrb='npm run build'
alias nrl='npm run lint'
alias nrt='npm run test'
alias nrsb='npm run serve-big'
# -----------------------
# Docker & Kubernetes
# -----------------------
alias k='kubectl'
alias d='docker'
alias dps='docker ps'

Apps

Opening an application through a terminal sounds cool!

Let’s add a few shortened for them too.

# -----------------------
# Apps
# -----------------------
alias google="open -a /Applications/Google\ Chrome.app"
alias gmail="google https://mail.google.com/"
alias slack="open -a /Applications/Slack.app"
alias pman="open -a /Applications/Postman.app"
alias enote="open -a /Applications/Evernote.app"

Bonus

Adding Quick EDITOR Support

Add two environment variables in~/.zshrc

  • EDITOR = A file editor (I use Sublime Text for quick editing)
  • CEDITOR = Code editor (I use Webstorm)
# Update ~/.zshrc file
# ...
# ...
# ===== ADDITION ======
# ----------------------
# CUSTOM ZSH CONFIG
# ----------------------
# File Editor (subl = Sublime Text)
export EDITOR="subl"
# Code Editor
export CEDITOR="webstorm"
# =====================
# ------------------------
# LOAD CUSTOM ALIASES FROM
# ------------------------
source $HOME/aliases.sh

Add a few handy aliases in ~/aliases.sh

# Opens a file/folder using default Editor
alias edit="$EDITOR"
# e.g: edit some-randome-file.sh
# Opens a file/folder using default Code Editor
alias cedit="$CEDITOR"
# e.g: cedit my-project-directory

Use Aliases to Quickly Edit and Reload .zshrc

# ------------------------
# Use Aliases to Quickly Edit and Reload .zshrc
# ------------------------
# Opens .zshrc file in EDITOR for editing
alias ez='edit $HOME/.zshrc'
# Reloads ZSH configs
alias sz='source $HOME/.zshrc'

GIST

A full list of aliases we used in this article is also available as a Gist here

Conclusion

Instead of remembering long commands, start using Aliases. It will save a great deal of typing when you are on the command line.

We saw several Aliases above that will boost your productivity and make your command line usage much easier.

As an exercise how about you review the tools and commands you use on daily basis and create aliases (and functions) for them? Do share with us here!

--

--

Shahnawaz Ali Kausar

Another technology enthusiast and a design pattern lover! — Senior Software Engineer at SECURITI.ai