Skip to content

Instantly share code, notes, and snippets.

@Sam-Spencer
Last active May 27, 2025 20:31
Show Gist options
  • Select an option

  • Save Sam-Spencer/8075bd7836008e23ff03473825abe15e to your computer and use it in GitHub Desktop.

Select an option

Save Sam-Spencer/8075bd7836008e23ff03473825abe15e to your computer and use it in GitHub Desktop.
Zsh Config with helper functions
# Setup PATH
# export PATH=$PATH:/Users/samspencer/bin
export GEM_HOME="$HOME/.gem"
export PATH=$GEM_HOME/bin:$PATH
export PATH="$HOME/.config/n/bin:$PATH"
export PATH="$HOME/.config/npm/bin:$PATH"
export PATH="$HOME/.bin:$PATH"
export PATH="$HOME/.deno/bin:$PATH"
export PATH="$PATH:/opt/custom"
export JNA_LIBRARY_PATH="$HOME/Library/JNA"
export JAVA_LIBRARY_PATH="$HOME/Library/CFM"
export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export CLASSPATH=$HOME/Library/JNA/jna-5.16.0.jar:$HOME/Library/JNA/jna-platform-5.16.0.jar:$CLASSPATH
export JNA_LIBRARY_PATH=$HOME/Library/JNA
export JNA_TMPDIR=$HOME/Library/JNA/native
eval "$(/opt/homebrew/bin/brew shellenv)"
eval "$(pyenv init -)"
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
# The next line updates PATH for the Google Cloud SDK.
if [ -f '/opt/gcloudsdk/path.zsh.inc' ]; then . '/opt/gcloudsdk/path.zsh.inc'; fi
# The next line enables shell command completion for gcloud.
if [ -f '/opt/gcloudsdk/completion.zsh.inc' ]; then . '/opt/gcloudsdk/completion.zsh.inc'; fi
# Setup ENV variables
export N_PREFIX=$HOME/.config/n
export PATH="${HOME}/.pyenv/shims:${PATH}"
# --- Custom Functions ---
# Switch node version in directory
#
sn() {
local version
version=$(n lsr --all | fzf)
if [ "x$version" != "x" ]
then
echo "Switching to Node $version"
n $version
fi
}
# Concise git rebase operation
#
gitrb() {
BOLD="\033[1m"
NONE="\033[0m"
if [[ $1 == "help" ]] || [[ $1 == "--help" ]] || [[ $1 == "-h" ]]
then
echo -e "\nRebases merges on the specified branch.\n\nPass the name of the branch to rebase on top of as the first and only argument. For example:\n\n\t${BOLD}gitrb main${NONE}\n"
else
git rebase --rebase-merges $1
fi
}
# Force Xcode Package Resolution
#
xres() {
xcodebuild -resolvePackageDependencies
}
# Install a simulator for use with Xcode
#
siminstall() {
BLUE="\033[34m"
RED="\033[31m"
NONE="\033[0m"
if [[ -f $1 ]]
then
echo "${BLUE}Installing simulator at path:${NONE} $1"
xcrun simctl runtime add $1
else
echo "${RED}$1 is not a valid file.${NONE}"
fi
}
# Create ICNS file from iconset folder
#
icns() {
BOLD="\033[1m"
NONE="\033[0m"
if [[ $1 == "help" ]] || [[ $1 == "--help" ]] || [[ $1 == "-h" ]]
then
echo -e "\nCreates an ICNS file from an iconset folder.\n\nPass the name of the folder, without an extension, as the first and only argument. For example:\n\n\t${BOLD}icns foldername${NONE}\n"
else
iconutil -c icns -- $1.iconset
fi
}
# Toggle System Low Priority Throttle
#
lthrottle() {
BOLD="\033[1m"
RED="\033[31m"
GREEN="\033[32m"
NONE="\033[0m"
if [[ $1 == "on" ]] || [[ $1 == "true" ]] || [[ $1 == "1" ]]
then
sudo sysctl debug.lowpri_throttle_enabled=1
echo "Low priority throttle is now ${BOLD}enabled${NONE} (${GREEN}system default${NONE})."
elif [[ $1 == "off" ]] || [[ $1 == "false" ]] || [[ $1 == "0" ]]
then
sudo sysctl debug.lowpri_throttle_enabled=0
echo "Low priority throttle is now ${BOLD}disabled${NONE}. ${RED}CPU usage may increase${NONE}."
else
echo "Unknown argument, $1. Expected 'on' or 'off'."
fi
}
# Create and navigate to a new directory
#
mcd() {
mkdir -pv $1
cd $1
}
# Compare two inputs
#
same() {
GREEN="\033[32m"
RED="\033[31m"
NONE="\033[0m"
if [[ $1 == $2 ]]
then
echo "${GREEN}Values are equal.${NONE}"
else
echo "${RED}Values are not equal.${NONE}"
fi
}
checkSha() {
GREEN="\033[32m"
RED="\033[31m"
YELLOW="\033[33m"
NONE="\033[0m"
# Check if at least two arguments are provided
if [[ $# -lt 2 ]]; then
echo "${YELLOW}Usage: check_sha <file_path> <expected_checksum> [sha_algorithm]${NONE}"
return 1
fi
local file_path="$1"
local expected_checksum="$2"
# Default SHA algorithm is 256 if not specified
local sha_alg="${3:-256}"
# Verify the file exists
if [[ ! -f "$file_path" ]]; then
echo "${RED}Error: File '$file_path' does not exist.${NONE}"
return 1
fi
# Calculate the checksum using shasum
local computed_checksum
computed_checksum=$(shasum -a "$sha_alg" "$file_path" | awk '{print $1}')
# Compare computed checksum with the expected one
if [[ "$computed_checksum" == "$expected_checksum" ]]; then
echo "${GREEN}Checksum OK: $computed_checksum matches the expected checksum.${NONE}"
return 0
else
echo "${RED}Checksum Mismatch: Computed $computed_checksum, but expected $expected_checksum.${NONE}"
return 1
fi
}
# Run pytest in the current directory
#
pyt() {
if [[ $1 == "-c" ]] || [[ $1 == "--cov" ]] || [[ $1 == "--cover" ]]
then
python -m coverage run -m pytest
else
python -m pytest
fi
}
# Flush the DNS Cache
#
dnsdrain() {
echo "Flushing DNS cache..."
sudo dscacheutil -flushcache
echo "Flushed DNS cache."
echo "Killing mDNSResponder..."
sudo killall -HUP mDNSResponder
echo "Killed mDNSResponder."
}
# Make a script executable
#
mkexe() {
GREEN="\033[32m"
RED="\033[31m"
NONE="\033[0m"
if [[ -f $1 ]]
then
chmod 755 $1
echo "${GREEN}$1 is now executable.${NONE}"
else
echo "${RED}$1 is not a valid file.${NONE}"
fi
}
# Check URL validity
#
isValidUrl() {
RED="\033[31m"
NONE="\033[0m"
local url="$1"
# Regular expression to match a basic URL structure
if [[ "$url" =~ ^https?://[a-zA-Z0-9.-]+(\.[a-zA-Z]{2,})+([/?].*)?$ ]]; then
return 0
else
echo "${RED}Provided URL is invalid: $url${NONE}"
return 1
fi
}
# Function to check if an executable is installed
#
isExecutableInstalled() {
local executable="$1"
if command -v "$executable" &>/dev/null; then
return 0 # Executable found
else
return 1 # Executable not found
fi
}
# Get Google Cloud Auth Token
#
gctoken() {
GREEN="\033[32m"
RED="\033[31m"
YELLOW="\033[33m"
NONE="\033[0m"
# Check if the URL is valid
if ! isValidUrl "$1"; then
return 1
fi
if ! isExecutableInstalled "gcloud"; then
echo "gcloud is not installed, or it isn't available on your PATH."
return 1
fi
output=$(gcloud auth print-identity-token --audiences=$1)
# Check if output was captured successfully
if [[ -z "$output" ]]; then
echo "${YELLOW }No output returned from Google Cloud :(... something must've gone wrong.${NONE}"
return 1
fi
if command -v pbcopy &>/dev/null; then
echo "$output" | pbcopy
echo "${GREEN}Successfully generated Google Cloud auth token, and copied to clipboard.${NONE}"
fi
if [[ $2 == "show" ]] || [[ $2 == "s" ]]
then
echo "$output"
fi
}
# Custom help description
#
helpme() {
if [[ $1 == "--edit" ]] || [[ $1 == "-e" ]] || [[ $1 == "e" ]] || [[ $1 == "edit" ]]
then
# Try Xcode, then Nova, then Zed, then fallback to TextEdit.
if open -Ra "Xcode" >/dev/null 2>&1; then
open -a Xcode ~/.zshrc
elif open -Ra "Nova" >/dev/null 2>&1; then
open -a Nova ~/.zshrc
elif open -Ra "Zed" >/dev/null 2>&1; then
open -a Zed ~/.zshrc
else
open -a TextEdit ~/.zshrc
fi
return
fi
if [[ $1 == "--refresh" ]] || [[ $1 == "-r" ]] || [[ $1 == "r" ]] || [[ $1 == "refresh" ]]
then
echo "Refreshing custom functions and aliases..."
source ~/.zshrc
return
fi
if [[ $1 == "--git" ]] || [[ $1 == "-g" ]] || [[ $1 == "g" ]] || [[ $1 == "git" ]]
then
echo "Opening remote ZSH profile Gist..."
open 'https://gist.github.com/Sam-Spencer/8075bd7836008e23ff03473825abe15e'
return
fi
BOLD="\033[1m"
BLUE="\033[34m"
GREEN="\033[32m"
YELLOW="\033[33m"
RED="\033[31m"
WHITE_B="\033[97m"
CYAN_B="\033[96m"
NONE="\033[0m"
__explainer="${BOLD}${CYAN_B}Available Custom Functions${NONE}
${BOLD}${WHITE_B}Package Managers${NONE}
${BLUE}sn${NONE} Switch ${BOLD}node${NONE} version in local directory
${BLUE}xres${NONE} Forces ${BOLD}Xcode${NONE} Swift package resolution
${BLUE}pyt${NONE} Run ${BOLD}pytest${NONE} in the current directory (pass -c for coverage)
${GREEN}bu${NONE} Upgrade ${BOLD}Homebrew${NONE} packages
${GREEN}gurgle${NONE} Run ${BOLD}./gradlew${NONE} in the current directory
${BLUE}siminstall${NONE} Installs the given OS Simulator for use with ${BOLD}Xcode${NONE}
${BOLD}${WHITE_B}Git Operations${NONE}
${BLUE}gitrb${NONE} Git rebase merges on the provided branch
${BOLD}${WHITE_B}File Management${NONE}
${BLUE}mcd${NONE} Create and navigate to a new directory
${GREEN}la${NONE} Pretty print all files in the current directory
${GREEN}mkexe${NONE} Make a given file executable
${BOLD}${WHITE_B}Comparing Values${NONE}
${BLUE}same${NONE} Compare two values to check if they're the same
${GREEN}equal${NONE} Run a Kaleidoscope comparison
${BLUE}checkSha${NONE} Compare the checksum of a file to the provided checksum
${BOLD}${WHITE_B}Miscellaneous${NONE}
${BLUE}icns${NONE} Generate an ICNS file from an iconset folder using the folder name
${BLUE}lthrottle${NONE} Toggles the system's low priority throttling (${RED}requires ${BOLD}sudo${NONE})
${BLUE}dnsdrain${NONE} Flushes the system's DNS cache and kills the DNS responder process (${RED}requires ${BOLD}sudo${NONE})
${GREEN}sysdiag${NONE} Runs system diagnostics (${RED}requires ${BOLD}sudo${NONE})
${BLUE}gctoken${NONE} Generates a Google Cloud auth token for use with Postman using the given audience.
To edit the available functions, run '${YELLOW}open ~/.zshrc${NONE}' OR pass the '${YELLOW}--edit${NONE}' flag to this function. When finished editing, save and refresh by passing the '${YELLOW}--refresh${NONE}' flag to this function. Get remotely-hosted profile details by passing the '${YELLOW}--git${NONE}' flag to this function.
"
echo -e "$__explainer"
}
# Setup Aliases
alias MSBuild.exe='msbuild'
alias la='ls -la -CF'
alias equal='ksdiff'
alias bu='brew upgrade'
alias help='helpme'
alias gurgle='./gradlew'
alias sysdiag='sudo sysdiagnose'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment