← All Articles

Use aliases to supercharge your bash and zsh terminal

I've collected a handful of aliases / shortcuts which I configure for my Bash or Zsh shell whenever I get a new machine - nothing fancy, just saves me a few milliseconds here and there and I've got used to them over time. Absolute milliseconds I tell you.

Bash or Zsh?

Before we get much further, just to point out that these apply whether you are using Bash or the Zsh shell found in the more recent Macs. For our purposes today, they both have the same functionality. The only significant difference is where to place these commands. If you are using bash, these would live in your ~/.bashrc or ~/.bash_profile files, while in Zsh they are kept in ~/.zshrc.

Docker commands

For work we use Docker containers configured for a number of different environments so naturally I have a few of these.
  
# Bring up/down containers, build containers in different environments
alias ddup="docker-compose -f ./dev/local/docker-compose.yml up -d"
alias ddown="docker-compose -f ./dev/local/docker-compose.yml down"
alias ddups="docker-compose -f ./dev/staging/docker-compose.yml up -d"
alias ddowns="docker-compose -f ./dev/staging/docker-compose.yml down"
alias dbuild="docker-compose -f ./dev/local/docker-compose.yml build"
alias dbuilds="docker-compose -f ./dev/staging/docker-compose.yml build"

# Read logs from a container
alias dlogs="docker logs -f my.container.name"

# Get on a bash prompt in a container
alias bweb="docker exec -it my.container.name bash"

# Remove any stuck/dangling containers
function dclear(){
  docker rm $(docker ps -q -f 'status=exited')
  docker rmi $(docker images -q -f "dangling=true")
}
export -f dclear
  

Kubernetes shortcuts

We also use k8s for orchestration, so there's a couple of Kubernetes commands thrown in there too.
  
# List all pods matching a pattern, in a specific namespace
function getpods() {
  kubectl get pods -n $1 | grep $2
}

# Enter a bash prompt on a specific pod, in a specific namespace
function enterpod() {
  kubectl exec -it $1 -n $2 bash
}
  
You'll notice the `$1` and `$2` in these functions, which get replaced by user input. For example, running the following:
  1. `getpods secretproject staging0` means that I will list all pods with a name like `staging0` in the `secretproject` namespace.
  2. `enterpod secretproject staging0-54654` means I get a bash prompt on the `staging0-54654` pod in the `secretproject` namespace.

Git commands

These are my commonly used Git commands:
  
alias gpo="git pull origin "
alias gcb="git checkout -B"
alias gcm="git checkout master"
alias gs="git status"
alias gsh="git stash"
alias gsp="git stash pop"
alias gb="git branch"
alias gp="git push"
alias gd="git diff"

# Get the last X commits
alias gn="git log -n "

# Useful to remove all non-master branches
alias gprune="git branch | grep -v 'master' | xargs git branch -D"
  
Note that these aliases can be used with inputs too. Some examples:
  1. To pull the `fancypants` branch from the remote repo, I would type `gpo fancypants`.
  2. To get the last 5 commits, I would type `gn 5`.

SSH shortcuts

I use a couple of different SSH keys to streamline logging in to different systems, so these just makes it somewhat faster to get around.
  
# Add / remove ssh keys as needed
alias ssh-personal-on="ssh-add ~/.ssh/personal"
alias ssh-personal-off="ssh-add -d ~/.ssh/personal"
alias ssh-work-on="ssh-add ~/.ssh/work"
alias ssh-work-off="ssh-add -d ~/.ssh/work"

# SSH on to a server
alias sandbox="ssh -A myname@sandbox.work.com"

# SSH on to a server with custom port and with specific key
alias myblog="ssh -p 8979 -i ~/.ssh/blog me@192.222.333.444"
  
Made with JoyBird