4 Handy ZSH Scripts for Everyday Development

We all spend a lot of time in the command line, so why not make it more efficient? Below are four ZSH helper scripts you can drop into your “.zshrc” to speed up common tasks like pruning stale Git branches, finding which processes are listening on your ports, reviewing recent Git branches, and extracting various archive formats.

1. gpurge

When working with Git, you might accumulate local branches that no longer exist on the remote. “gpurge” fetches the latest data and prunes these “gone” branches in one sweep.


gpurge() {
git fetch -p && \
git branch -vv | grep ': gone]' | awk '{print $1}' | xargs git branch -D
}

How to use it:

  1. Ensure you’re in a Git repository.
  2. Run “gpurge” in your terminal.
  3. Stale local branches that have been removed from the remote are deleted.

Why it’s useful:

It prevents you from hoarding outdated branches that clutter your local repo.

2. listening

 “listening” filters lsof output to show which processes are listening on TCP ports.

listening() {
lsof -i -n -P | grep --color=auto \
--exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} TCP | \
grep --color=auto --exclude-dir={.bzr,CVS,.git,.hg,.svn,.idea,.tox} LISTEN
}

How to use it:

  1. Run “listening” in your terminal.
  2. You’ll see a list of processes and the ports they’re listening on.

Why it’s useful:

Ever wonder why a certain port is in use or which application is listening? This script helps you quickly track down the culprit.

3. gitrecent

“gitrecent” lists your most recent local Git branches — handy for jumping back to a branch you were working on a few days ago without scanning through the entire list.


gitrecent() {
git for-each-ref --sort=-committerdate refs/heads/ \
--format="%(refname:short) - %(committerdate:relative)" | head -n 10
}

How to use it:

Run “gitrecent” inside a Git repo to see up to 10 local branches, sorted by most recent commit.

Why it’s useful:

You can avoid mentally parsing all your branches. This one-liner surfaces only the ones you’ve committed to most recently.

4. extract

“extract” detects the archive type (tar, zip, 7z, etc.) and extracts it with the correct command. No more looking up which flags to use.

extract() {
if [ -f "$1" ] ; then
case "$1" in
*.tar.bz2) tar xjf "$1" ;;
*.tar.gz) tar xzf "$1" ;;
*.bz2) bunzip2 "$1" ;;
*.rar) unrar x "$1" ;;
*.gz) gunzip "$1" ;;
*.tar) tar xf "$1" ;;
*.tbz2) tar xjf "$1" ;;
*.tgz) tar xzf "$1" ;;
*.zip) unzip "$1" ;;
*.Z) uncompress "$1";;
*.7z) 7z x "$1" ;;
*) echo "'$1' cannot be extracted via extract()" ;;
esac
else
echo "'$1' is not a valid file"
fi
}

How to use it:

  1. Run “extract” file.tar.gz (or .zip, .rar, etc.).
  2. The function detects the extension and handles extraction automatically.

Why it’s useful:

No need to memorize the correct command and flags for each archive type. One function does it all.

Putting it all together

To add these functions to your shell environment:

  1. Open your ~/.zshrc in a text editor.
  2. Copy and paste the four functions into the file.
  3. Save and close the file.
  4. Reload your shell by running the following:

source ~/.zshrc

These scripts address common developer chores — clearing stale Git branches, finding processes hogging ports, checking recent branches, and extracting archives. Feel free to tweak them for your own needs like adding color-coded outputs or extra safety checks, or integrate them with your favorite tooling.

Conversation
  • Michael Swieton says:

    Thanks, Matt – This was exactly what I needed this morning.

  • Join the conversation

    Your email address will not be published. Required fields are marked *