Files
home/.zshrc
T
valknarandClaude Sonnet 4.6 eeed7f3566 fix: source ~/.env with set -a so variables are exported to child processes
Variables set in ~/.env without explicit export were shell-local only —
child processes (scripts, subshells) never inherited them. Source it in
a dedicated section with set -a/set +a to auto-export everything.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-06-22 18:28:46 +02:00

156 lines
3.8 KiB
Bash

# ~/.zshrc
# =============================================================================
# HELPER FUNCTIONS
# =============================================================================
# Add a directory to PATH with existence check
# Usage: add_to_path /path/to/add [prepend|append]
add_to_path() {
local dir="$1"
local position="${2:-prepend}"
if [[ -d "$dir" ]]; then
case "$position" in
prepend)
export PATH="$dir:$PATH"
;;
append)
export PATH="$PATH:$dir"
;;
*)
echo "Warning: Unknown position '$position' for path '$dir'" >&2
return 1
;;
esac
fi
}
# Source a file with existence and readability check (zsh-friendly)
# Usage: source_file /path/to/file
source_file() {
local file="$1"
if [[ -f "$file" && -r "$file" ]]; then
source "$file"
fi
}
# Eval the output of a command, silently skipping if it fails or produces nothing
# Usage: eval_expr 'pyenv init -'
eval_expr() {
local cmd="$1"
local output
if output="$(eval "$cmd" 2>/dev/null)" && [[ -n "$output" ]]; then
eval "$output"
fi
}
# =============================================================================
# PATH EXTENSIONS
# =============================================================================
# Define all path extensions in an array for easy management
path_extensions=(
"$HOME/bin"
"$HOME/.local/bin"
"$HOME/.rbenv/bin"
"$HOME/.pyenv/bin"
"$HOME/.cargo/bin"
"$HOME/go/bin"
)
# Apply add_to_path to each directory (prepend by default)
for path_dir in "${path_extensions[@]}"; do
add_to_path "$path_dir" prepend
done
# =============================================================================
# EVAL EXPRESSIONS
# =============================================================================
# Commands whose output should be eval'd (tool initializers, etc.)
eval_expressions=(
'pyenv init -'
)
for expr in "${eval_expressions[@]}"; do
eval_expr "$expr"
done
# =============================================================================
# ENVIRONMENT VARIABLES
# =============================================================================
# Source ~/.env with set -a so every variable is automatically exported
# and inherited by child processes (scripts, subshells, etc.).
if [[ -f "$HOME/.env" && -r "$HOME/.env" ]]; then
set -a
source "$HOME/.env"
set +a
fi
# =============================================================================
# CONFIGURATION FILES TO SOURCE
# =============================================================================
# Define files to source in an array for easy management
source_files=(
"$HOME/.nvm/nvm.sh"
"$HOME/.cargo/env"
"$HOME/.oh-my-zsh/oh-my-zsh.sh"
"$HOME/.p10k.zsh"
)
# Source zsh-compatible files (skip if missing, no warnings)
for config_file in "${source_files[@]}"; do
source_file "$config_file"
done
# =============================================================================
# SHELL OPTIONS
# =============================================================================
zsh_options=(
appendhistory
incappendhistory
sharehistory
histignoreall_dups
histreduceblanks
complete_in_word
always_to_end
prompt_percent
prompt_subst
auto_cd
)
for opt in "${zsh_options[@]}"; do
setopt "$opt"
done
# =============================================================================
# ALIASES AND FUNCTIONS
# =============================================================================
# Please add your custom aliases and functions here or in:
# - $ZSH_CUSTOM/aliases.zsh
# - $ZSH_CUSTOM/functions.zsh
type bat >/dev/null 2>&1 && alias cat="bat --paging=never"
function y() {
local tmp="$(mktemp -t "yazi-cwd.XXXXXX")" cwd
command yazi "$@" --cwd-file="$tmp"
IFS= read -r -d '' cwd < "$tmp"
[ "$cwd" != "$PWD" ] && [ -d "$cwd" ] && builtin cd -- "$cwd"
command rm -f -- "$tmp"
}