My .zshrc file. Well, parts of it, anyway.
First things first, I use oh-my-zsh
export ZSH="$HOME/.oh-my-zsh"
My plugins and theme:
plugins=(git themes emacs emoji macos iterm2 kubectl thefuck web-search man) ZSH_THEME=ys
Of course, actually source omz:
source $ZSH/oh-my-zsh.sh
A line added by rbenv. I could just remove the test since I have it on all my machines …
which rbenv >/dev/null 2>&1 && eval "`rbenv init -`"
And have a more useful history
alias history='history -i' eval "$(atuin init zsh)"
This makes its output look like this:
$ history|tail -1 5461 2025-04-01 20:23 ls
Also, atuin makes reverse search (Ctrl-R) much better IMHO.
On my work machine I frequently add new functions to my zshrc file to help me do whatever I need to do more efficiently. For this reason, I find it useful to have my zshrc autload every time its modified (autoloaded here means after I run a command or press enter on that shell):
# Function to calculate checksum of .zshrc get_zshrc_checksum() { md5 -q ~/.zshrc 2>/dev/null || md5sum ~/.zshrc 2>/dev/null | cut -d' ' -f1 } # Store the initial checksum of .zshrc ZSHRC_CHECKSUM=$(get_zshrc_checksum) # Function to reload .zshrc reload_zshrc() { source ~/.zshrc echo "Reloaded .zshrc" } # Function to check if .zshrc was modified check_zshrc_updated() { local current_checksum=$(get_zshrc_checksum) if [[ "$current_checksum" != "$ZSHRC_CHECKSUM" ]]; then echo ".zshrc has been modified, reloading..." ZSHRC_CHECKSUM=$current_checksum reload_zshrc fi } # Add the function to the precmd hook to run before each prompt autoload -Uz add-zsh-hook add-zsh-hook precmd check_zshrc_updated
Sometimes I define variables, aliases, or short-lived functions (that aren't worth putting on the file for posterity) and I want those to be available across several shells (I use lots of tabs):
## manage shared state / temp functions # Define the shared state directory SHARED_STATE_DIR="${HOME}/.zsh_shared_state" # Create the directory if it doesn't exist [[ -d "$SHARED_STATE_DIR" ]] || mkdir -p "$SHARED_STATE_DIR" # Function to load all shared state files load_shared_state() { local file # Source all files in the shared state directory for file in "$SHARED_STATE_DIR"/*; do [[ -f "$file" ]] && source "$file" done } # Function to add a function to the shared state shared_function() { local func_name="$1" local file_path="$SHARED_STATE_DIR/func_${func_name}" # Check if the function exists if ! typeset -f "$func_name" > /dev/null; then echo "Function '$func_name' does not exist" >&2 return 1 fi # Get function definition and save it to a file typeset -f "$func_name" > "$file_path" echo "Function '$func_name' added to shared state" } # Function to add an alias to the shared state shared_alias() { local alias_name="$1" local file_path="$SHARED_STATE_DIR/alias_${alias_name}" # Get the alias definition local alias_def=$(alias "$alias_name" 2>/dev/null) if [[ -z "$alias_def" ]]; then echo "Alias '$alias_name' does not exist" >&2 return 1 fi # Save alias to a file echo "$alias_def" > "$file_path" echo "Alias '$alias_name' added to shared state" } # Function to add a variable to the shared state shared_variable() { local var_name="$1" local file_path="$SHARED_STATE_DIR/var_${var_name}" local value="${(P)var_name}" # Get the value of the variable if [[ -z "$var_name" ]]; then echo "Variable name is required" >&2 return 1 fi # Save variable to a file, properly escaping special characters escaped_value=$(printf '%q' "$value") echo "export $var_name=$escaped_value" > "$file_path" echo "Variable '$var_name' added to shared state" } # Function to remove an item from the shared state shared_remove() { local item_type="$1" local item_name="$2" if [[ -z "$item_type" || -z "$item_name" ]]; then echo "Usage: shared_remove <function|alias|variable> <name>" >&2 return 1 fi local prefix case "$item_type" in function) prefix="func_" ;; alias) prefix="alias_" ;; variable) prefix="var_" ;; *) echo "Unknown item type: $item_type. Use function, alias, or variable." >&2 return 1 ;; esac local file_path="$SHARED_STATE_DIR/${prefix}${item_name}" if [[ -f "$file_path" ]]; then rm -f "$file_path" echo "Removed $item_type '$item_name' from shared state" else echo "$item_type '$item_name' not found in shared state" >&2 return 1 fi } # Function to list all shared items shared_list() { local item_type="$1" echo "Shared State Contents:" echo "----------------------" if [[ -z "$item_type" || "$item_type" == "function" ]]; then echo "Functions:" for file in "$SHARED_STATE_DIR"/func_*; do [[ -f "$file" ]] && echo " $(basename "$file" | sed 's/^func_//')" done echo "" fi if [[ -z "$item_type" || "$item_type" == "alias" ]]; then echo "Aliases:" for file in "$SHARED_STATE_DIR"/alias_*; do [[ -f "$file" ]] && echo " $(basename "$file" | sed 's/^alias_//')" done echo "" fi if [[ -z "$item_type" || "$item_type" == "variable" ]]; then echo "Variables:" for file in "$SHARED_STATE_DIR"/var_*; do [[ -f "$file" ]] && echo " $(basename "$file" | sed 's/^var_//')" done echo "" fi } # Function to view the content of a shared item shared_view() { local item_type="$1" local item_name="$2" if [[ -z "$item_type" || -z "$item_name" ]]; then echo "Usage: shared_view <function|alias|variable> <name>" >&2 return 1 fi local prefix case "$item_type" in function) prefix="func_" ;; alias) prefix="alias_" ;; variable) prefix="var_" ;; *) echo "Unknown item type: $item_type. Use function, alias, or variable." >&2 return 1 ;; esac local file_path="$SHARED_STATE_DIR/${prefix}${item_name}" if [[ -f "$file_path" ]]; then echo "Content of $item_type '$item_name':" echo "-----------------------------" cat "$file_path" else echo "$item_type '$item_name' not found in shared state" >&2 return 1 fi } reload_shared_state() { load_shared_state } autoload -Uz add-zsh-hook add-zsh-hook precmd reload_shared_state load_shared_state
Good old pathmunge:
pathmunge() { if ! echo $PATH | /usr/bin/egrep -q "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi fi }
Load other scripts into the path:
pathmunge ~/gdrive/scripts
I took this idea from Percona Toolkit. Just set _DEBUG to something and have your code show lots of debug info. With this, freely sprinkle debugp in your shell code and only have its output show up if _DEBUG is set:
debugp() { [ -n "$_DEBUG" ] && echo "$*">&2 }
I don't use this too often now as I usually just ask Siri (typing, not speaking), but I haven't removed it yet:
remind_me() { ( wait_time=$(( $1 * 60)) shift sleep $wait_time osascript -e "tell app \"System Events\" to display dialog \"$*\" buttons {\"OK\"} default button 1 with title \"remind_me notification\"" ) & }
For when the need arises …
kalishell() { kali=$(docker ps |grep kali|grep -v CONTAINER|awk '{print $1}') [ -z "$kali" ] && docker run --tty --interactive --name kali kalilinux/kali-rolling && echo "apt update && apt -y install kali-linux-headless" docker exec -it $kali /bin/bash }
Because I often build lists dynamically and it's easier to get the last comma out with sed than with an exception in the loop:
remove_last_comma_from_input() { sed '$s/,$//' }
Basic parallelization. Very basic.
run_in_background_if_cnt_of_background_processes_is_less_than() { [ -z "$1" ] && echo "expected arg: max number of background processes" && return 1 [ -z "$2" ] && echo "expected arg: command to run" && return 1 [ -z "$3" ] && echo "expected arg: command to run" && return 1 max_processes=$1 shift while true do running_processes=$(jobs -p|wc -l) if [ $running_processes -lt $max_processes ] then $* & else sleep 1 fi done }
Besides having the file on git (not on github, not that I have anything against them), make manual copies. One can never be too much paranoid.
backup() { 'cp' ~/.zshrc ~/gdrive/backup/dotfiles/$(hostname -s)/ }
Sometimes I forget if I already have a function defined for something I want to do …
my_functions() { grep -E '^[a-zA-Z0-9_]+\(\)' ~/.zshrc }
Dear Python:
export PYENV_ROOT="$HOME/.pyenv" export PYENV_ROOT="$HOME/.pyenv" export PATH="$PYENV_ROOT/bin:$PATH" eval "$(pyenv init --path)" eval "$(pyenv init -)"