~/blog/zshrc-configuration-guide

Boost Your Productivity: My Zshrc Configuration for Platform Engineers

4 min read
Zshrc Configuration for Platform Engineers

As a Platform Engineer, the terminal is my home. Efficiently navigating clusters, decoding secrets, and managing contexts can save hours every week. Over time, I've curated a ~/.zshrc file that acts as my Swiss Army knife.

In this post, I'll share some of the most useful functions and aliases I use daily. You can also view or download my full raw .zshrc here.

A Note on Open Source Tools

Before diving into the custom functions, I have to give a massive shout-out to Ahmet Alp Balkan. Tools like kubectx, kubens, and krew are absolute lifesavers that we use every single day. If you aren't using them yet, go install them immediately. They form the backbone of a sane Kubernetes workflow.

Platform Engineering Utilities

One of the biggest pain points is context switching between clusters and cloud providers. I use a ctx function to give me an immediate heads-up of where I am.

#@ util: show all platform contexts
ctx() {
  echo "☸️  K8s Context: $(kubectl config current-context 2>/dev/null || echo 'none')"
  echo "☸️  K8s NS:      $(kubectl config view --minify -o jsonpath='{..namespace}' 2>/dev/null || echo 'default')"
  echo "☁️  GCP Project: $(gcloud config get-value project 2>/dev/null || echo 'none')"
  echo "🔶 AWS Profile: ${AWS_PROFILE:-default}"
  echo "🔷 Azure Sub:   $(az account show --query name -o tsv 2>/dev/null || echo 'none')"
  echo "📁 TF Workspace: $(terraform workspace show 2>/dev/null || echo 'N/A')"
}
#@ util: show all platform contexts
ctx() {
  echo "☸️  K8s Context: $(kubectl config current-context 2>/dev/null || echo 'none')"
  echo "☸️  K8s NS:      $(kubectl config view --minify -o jsonpath='{..namespace}' 2>/dev/null || echo 'default')"
  echo "☁️  GCP Project: $(gcloud config get-value project 2>/dev/null || echo 'none')"
  echo "🔶 AWS Profile: ${AWS_PROFILE:-default}"
  echo "🔷 Azure Sub:   $(az account show --query name -o tsv 2>/dev/null || echo 'none')"
  echo "📁 TF Workspace: $(terraform workspace show 2>/dev/null || echo 'N/A')"
}

Running ctx instantly tells me if I'm about to accidentally delete a production resource or if I'm safely in a dev environment.

Kubernetes Shortcuts

Typing kubectl commands repeatedly is a recipe for carpal tunnel. Here are a few aliases and functions to speed things up.

Pod IPs and Debugging

Quickly listing pod IPs and nodes can be crucial during an outage.

#@ k8s-debug: list pod IPs
kpod-ips() {
  kubectl get pods -o custom-columns="NAME:.metadata.name,IP:.status.podIP,NODE:.spec.nodeName"
}
#@ k8s-debug: list pod IPs
kpod-ips() {
  kubectl get pods -o custom-columns="NAME:.metadata.name,IP:.status.podIP,NODE:.spec.nodeName"
}

Decoding Secrets

I often need to check a secret value. Instead of getting the JSON and running it through base64 manually, I use kdecode.

#@ util: decode k8s secret
kdecode() {
  local secret=$1
  local key=$2
  if [[ -z "$key" ]]; then
    kubectl get secret "$secret" -o json | jq -r '.data | to_entries[] | "\(.key): \(.value | @base64d)"'
  else
    kubectl get secret "$secret" -o jsonpath="{.data.$key}" | base64 -d
  fi
}
#@ util: decode k8s secret
kdecode() {
  local secret=$1
  local key=$2
  if [[ -z "$key" ]]; then
    kubectl get secret "$secret" -o json | jq -r '.data | to_entries[] | "\(.key): \(.value | @base64d)"'
  else
    kubectl get secret "$secret" -o jsonpath="{.data.$key}" | base64 -d
  fi
}

[!NOTE] These functions assume you have standard tools like jq and kubectl installed on your machine.

Usage: kdecode my-secret-name to dump all keys, or kdecode my-secret-name password to get just the password.

GitHub Copilot CLI Integration

I also leverage the GitHub Copilot CLI directly in my shell. Sometimes you know what you want to do but not the exact ffmpeg flags.

#@ util: GitHub Copilot Suggest
ghcs() {
  # ... wrapper logic ...
  gh copilot suggest -t shell "$@"
}
#@ util: GitHub Copilot Suggest
ghcs() {
  # ... wrapper logic ...
  gh copilot suggest -t shell "$@"
}

(Note: This is an abbreviated wrapper. See the full file for the complete function that handles execution logic)

Simple aliases keep the flow smooth:

  • zshrc: Opens the configuration file in my default editor.
  • reload: Sources the file and prints a confirmation.
  • .., ...: Quick directory navigation.
  • gswc: Switch git branch and create new branch
  • gswm: Checkout main branch and pull latest changes
  • kn: Switch k8s namespace
  • kc: Switch k8s context
  • kpod-ips: List pod IPs
  • testbox: netshoot pod for debugging network issues

Installation

# Backup your current configuration
cp ~/.zshrc ~/.zshrc.bak
 
# Append the configuration from the remote source
curl -s https://emrecavunt.com/files/zshrc.txt >> ~/.zshrc
 
# Reload the shell to apply changes
reload # or source ~/.zshrc
# Backup your current configuration
cp ~/.zshrc ~/.zshrc.bak
 
# Append the configuration from the remote source
curl -s https://emrecavunt.com/files/zshrc.txt >> ~/.zshrc
 
# Reload the shell to apply changes
reload # or source ~/.zshrc

Conclusion

Your shell configuration should evolve with you. Don't be afraid to add aliases for commands you run more than twice a day.

Check out the full configuration, gist.github.com/emrecavunt to see the rest of the aliases, including fzf integrations and more platform-specific helpers.