ZSH aliases that will change your life

Evan Frawley
May 29, 2020
8m read
views

The zsh aliases that'll turn you into a "10x Engineer"

As a software engineer, spending time in the command line has become second nature to me. Doing any work related to git? Command line. Running whois against a domain to see which asshole took the domain name that I just came up with. Command line. Running a local project, ssh-ing into a remote box, creating/moving/reading/deleting/renaming files, scraping data with a python script? All command line.

However, with nearly every developer using the command line, I only notice a small percentage of my peers customizing and tricking out their command line to maximize their efficiency. Often times I find myself running the same commands over and over, whether that's for debugging, checking the status of things, or simply my regular workflow. I feel like I can personally whiz my way around my instance of ITerm2 only because I've nailed down my aliases and keybinds.

Therefore, I wanted to share some of my favorite aliases and tricks that have boosted my efficiency over the years.

(First things first though -- I highly recommend using ohmyz.sh and the robbyrussel theme.)

Navigation aliases

alias gg="cd ~/code" # goes to my code directory
alias ggo="cd ~/go/src/github.com" # goes to go github source path
alias ggg="cd ~/go/src/github.com/GH_USERNAME/REPO" # goes to a specific project repo path

Simple Git shortcuts

# This is the one that I far an away use the most!
# Go back to last branch
alias back='git checkout @{-1}'
# These are great as well
# Git log
alias log='git log'
# Git status
alias gs='git status'
# Git diff
alias gd='git diff'
# Go to staging branch
alias staging='git checkout staging'
# All git branches
alias allbranch='git branch -a'
# Local git branches
alias branch='git branch'

More complex Git shortcuts

# Squashes most recent commit up to and including given commit hash
# usage: FIRST: git lot -> find the oldest commit hash you want to include
# squash COMMIT_HASH
function squash {
git reset --soft $1; git commit -a --amend --no-edit;
}
# Amend a commit message
# usage: amend "some new commit message"
function amend {
git commit --amend -m $1;
}
# Typical workflow that I'd have would be on a feature branch to, once the branch
# is ready to be committed to the main branch, I run `squash` and then `amend` with a message
# like `amend "Feature Branch: Feature Name"`

Docker Shortcuts

# Get container process
alias dps="docker ps"
# Get process included stopped containers
alias dpa="docker ps -a"
# Get container logs
# usage: dlogs my-container
function dlogs() { docker logs $1 }
# Remove all containers
function drmf() { docker rm -f $(docker ps -a -q); }

The meta stuff

# Open this file in VS Code
alias oz='code ~/.zshrc'
# Refreshes zsh profile
alias ref='source ~/.zshrc'

Whenever I want to edit my .zshrc, it's as easy as running oz in the CLI and as soon as I save the file, running ref will immediately apply the changes.

And.... Voilà! You now can pick and choose the ways that you can customize and trick out your CLI. Hopefully doing this over the years after making some changes you can cumulatively shave hours off of your development time 😄

Notes: I was inspired to write this after seeing this article and learning a few tricks!

Made with ❤️ and ☕️