V2 of my ZSH config
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

220 lines
6.4 KiB

#!/bin/zsh
# -------------------------
# Resolve this config's real directory
# -------------------------
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
symlink_dir="$(cd "$(dirname "$(readlink -f "${(%):-%N}")")" && pwd)"
else
# macOS does not ship GNU readlink -f by default.
symlink_dir="$(cd "$(dirname "$(readlink "${(%):-%N}")")" && pwd)"
fi
# -------------------------
# Private, machine-local configuration
# -------------------------
# These files are intentionally not part of this repository.
# Use them for secrets, tokens, private exports, work paths, and host-specific setup.
[[ -f "$HOME/.bash_private" ]] && source "$HOME/.bash_private"
[[ -f "$HOME/.private" ]] && source "$HOME/.private"
[[ -f "$HOME/.zsh_private" ]] && source "$HOME/.zsh_private"
# Do not source ~/.inputrc here.
# It is a readline config file, not shell code.
# -------------------------
# OS-specific configuration
# -------------------------
if [[ "$OSTYPE" == "darwin"* ]]; then
[[ -f "$HOME/.macrc" ]] && source "$HOME/.macrc"
fi
# -------------------------
# Local directories
# -------------------------
if [[ ! -d "$HOME/projects" ]]; then
echo "Making $HOME/projects"
mkdir -p "$HOME/projects"
fi
if command -v go >/dev/null 2>&1; then
mkdir -p "$HOME/.go"
fi
# -------------------------
# Git completion
# -------------------------
# Auto-installs Git completion files from a pinned Git commit.
# This avoids fetching from a moving branch like master.
#
# To fill in the placeholders:
#
# cd /tmp
# git clone https://github.com/git/git.git git-src
# cd git-src
# git checkout v2.45.2
# git rev-parse HEAD
# shasum -a 256 contrib/completion/git-completion.bash
# shasum -a 256 contrib/completion/git-completion.zsh
#
# Then paste the commit and hashes below.
ZSH_CACHE_DIR="$HOME/.zsh"
mkdir -p "$ZSH_CACHE_DIR"
GIT_COMPLETION_COMMIT="bea9ecd24b0c3bf06cab4a851694fe09e7e51408"
GIT_COMPLETION_BASH_URL="https://raw.githubusercontent.com/git/git/${GIT_COMPLETION_COMMIT}/contrib/completion/git-completion.bash"
GIT_COMPLETION_ZSH_URL="https://raw.githubusercontent.com/git/git/${GIT_COMPLETION_COMMIT}/contrib/completion/git-completion.zsh"
GIT_COMPLETION_BASH_FILE="$ZSH_CACHE_DIR/git-completion.bash"
GIT_COMPLETION_ZSH_FILE="$ZSH_CACHE_DIR/_git"
GIT_COMPLETION_BASH_SHA256="e667d8fdc0f0071833c94ccb2565a503bf413da0ae28d74efe2e70e2beb1c4c6"
GIT_COMPLETION_ZSH_SHA256="f6075d283250785edcf09281992bde6b4bcc43eb2dca74488d880a52f7659c09"
_zshv2_sha256() {
if command -v sha256sum >/dev/null 2>&1; then
sha256sum "$1" | awk '{print $1}'
elif command -v shasum >/dev/null 2>&1; then
shasum -a 256 "$1" | awk '{print $1}'
else
echo "No SHA-256 tool found. Need sha256sum or shasum." >&2
return 1
fi
}
_zshv2_download_verified() {
local url="$1"
local dest="$2"
local expected_sha="$3"
local tmp_file
local actual_sha
tmp_file="$(mktemp "${dest}.tmp.XXXXXX")" || return 1
if ! command -v curl >/dev/null 2>&1; then
echo "curl is required to install Git completion files." >&2
rm -f "$tmp_file"
return 1
fi
if ! curl --fail --location --silent --show-error --output "$tmp_file" "$url"; then
echo "Failed to download $url" >&2
rm -f "$tmp_file"
return 1
fi
actual_sha="$(_zshv2_sha256 "$tmp_file")" || {
rm -f "$tmp_file"
return 1
}
if [[ "$actual_sha" != "$expected_sha" ]]; then
echo "SHA-256 mismatch for $url" >&2
echo "Expected: $expected_sha" >&2
echo "Actual: $actual_sha" >&2
rm -f "$tmp_file"
return 1
fi
mv "$tmp_file" "$dest"
}
_zshv2_file_verified() {
local file="$1"
local expected_sha="$2"
local actual_sha
[[ -f "$file" ]] || return 1
actual_sha="$(_zshv2_sha256 "$file")" || return 1
[[ "$actual_sha" == "$expected_sha" ]]
}
if [[ "$GIT_COMPLETION_COMMIT" != "PUT_PINNED_GIT_COMMIT_HASH_HERE" ]] &&
[[ "$GIT_COMPLETION_BASH_SHA256" != "PUT_SHA256_FOR_GIT_COMPLETION_BASH_HERE" ]] &&
[[ "$GIT_COMPLETION_ZSH_SHA256" != "PUT_SHA256_FOR_GIT_COMPLETION_ZSH_HERE" ]]; then
if ! _zshv2_file_verified "$GIT_COMPLETION_BASH_FILE" "$GIT_COMPLETION_BASH_SHA256"; then
_zshv2_download_verified "$GIT_COMPLETION_BASH_URL" "$GIT_COMPLETION_BASH_FILE" "$GIT_COMPLETION_BASH_SHA256"
fi
if ! _zshv2_file_verified "$GIT_COMPLETION_ZSH_FILE" "$GIT_COMPLETION_ZSH_SHA256"; then
_zshv2_download_verified "$GIT_COMPLETION_ZSH_URL" "$GIT_COMPLETION_ZSH_FILE" "$GIT_COMPLETION_ZSH_SHA256"
fi
else
echo "Git completion auto-install skipped: pinned commit and SHA-256 values are not configured." >&2
fi
if [[ -f "$GIT_COMPLETION_BASH_FILE" ]]; then
zstyle ':completion:*:*:git:*' script "$GIT_COMPLETION_BASH_FILE"
fi
fpath=("$ZSH_CACHE_DIR" $fpath)
autoload -Uz compinit
compinit
# -------------------------
# Pull in modular config
# -------------------------
source "$symlink_dir/aliases.zsh"
source "$symlink_dir/functions.zsh"
source "$symlink_dir/exports.zsh"
source "$symlink_dir/initializations.zsh"
source "$symlink_dir/ps1.zsh"
source "$symlink_dir/keybinds.zsh"
# -------------------------
# Language/tool initialization
# -------------------------
# Rust
[[ -f "$HOME/.cargo/env" ]] && source "$HOME/.cargo/env"
# Haskell / GHCup
[[ -f "$HOME/.ghcup/env" ]] && source "$HOME/.ghcup/env"
# OCaml / opam
[[ -r "$HOME/.opam/opam-init/init.sh" ]] && . "$HOME/.opam/opam-init/init.sh" >/dev/null 2>/dev/null || true
# -------------------------
# ZSH syntax highlighting
# -------------------------
# This still supports an existing local clone under:
#
# $HOME/.zsh/zsh-syntax-highlighting
#
# But it does not automatically clone during shell startup.
# Install with:
#
# brew install zsh-syntax-highlighting
#
# or:
#
# sudo apt install zsh-syntax-highlighting
#
# It must be sourced last.
if [[ -f "$HOME/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
source "$HOME/.zsh/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
elif [[ -f "/opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
source "/opt/homebrew/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
elif [[ -f "/usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
source "/usr/local/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
elif [[ -f "/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" ]]; then
source "/usr/share/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh"
fi
# -------------------------
# Deduplicate PATH
# -------------------------
typeset -U path PATH