#!/usr/bin/env zsh # ── core.zsh : environment, PATH, history, perf, plugin loader, integrations ── # ── environment & PATH ─────────────────────────────────────────────────────── # Homebrew (Apple Silicon) — guarded so Linux skips it cleanly. if [[ -x /opt/homebrew/bin/brew ]]; then export HOMEBREW_PREFIX=/opt/homebrew export HOMEBREW_CELLAR=/opt/homebrew/Cellar export HOMEBREW_REPOSITORY=/opt/homebrew path=(/opt/homebrew/bin /opt/homebrew/sbin $path) [[ -d /opt/homebrew/lib/ruby/gems ]] && \ path=(/opt/homebrew/lib/ruby/gems/*/bin(N) $path) fi export HOMEBREW_NO_AUTO_UPDATE=1 # PATH additions (zsh `path` array; `typeset -U` auto-dedupes). typeset -U path PATH path=("$HOME/.local/bin" "$HOME/.dotfiles/bin" "$HOME/.cargo/bin" "$HOME/go/bin" $path) # Colors export CLICOLOR=1 export LSCOLORS=ExGxBxDxCxEgEdxbxgxcxd # Editor preference: neovim > helix > vim > vi if (( $+commands[nvim] )); then export EDITOR=nvim VISUAL=nvim elif (( $+commands[hx] )); then export EDITOR=hx VISUAL=hx elif (( $+commands[vim] )); then export EDITOR=vim VISUAL=vim else export EDITOR=vi VISUAL=vi; fi # GPG export GPG_TTY=$TTY # ── history ────────────────────────────────────────────────────────────────── HISTFILE="$HOME/.zsh_history" HISTSIZE=100000 SAVEHIST=100000 setopt SHARE_HISTORY INC_APPEND_HISTORY HIST_IGNORE_ALL_DUPS \ HIST_IGNORE_SPACE HIST_REDUCE_BLANKS HIST_VERIFY EXTENDED_HISTORY setopt AUTO_CD AUTO_PUSHD PUSHD_IGNORE_DUPS INTERACTIVE_COMMENTS NO_BEEP # ── completion : single cached compinit ────────────────────────────────────── # Rebuild the dump at most once/day; otherwise fast-path with -C (skip security # scan). Counter proves we never double-init (the old config's bug). typeset -gi _CORE_COMPINIT_COUNT=0 () { # (#q...) inside [[ ]] is filename generation, which needs EXTENDED_GLOB. # Without it the -mh+24 test below is a literal string -> always true -> the # `compinit -C` fast path was dead and every shell paid the full security # scan. local_options restores the entry-time option state on return. setopt local_options extended_glob local zcd="$HOME/.cache/zsh/zcompdump" [[ -d ${zcd:h} ]] || mkdir -p ${zcd:h} autoload -Uz compinit # extra completion dirs (docker, custom) # zsh-completions must join fpath BEFORE compinit: compinit registers # autoloads at the moment it runs, so a later append is never seen. Listed # last so the user's own completion dirs still win. fpath=("$HOME/.dotfiles/completions"(N) "$HOME/.docker/completions"(N) "$HOME/.zsh/completions"(N) "${ZDOTDIR:-$HOME/.dotfiles}/config/plugins/zsh-completions/src"(N) $fpath) if [[ -n ${zcd}(#qN.mh+24) ]]; then compinit -d "$zcd" # dump >24h old → full rebuild else compinit -C -d "$zcd" # fresh → fast path fi (( _CORE_COMPINIT_COUNT++ )) } zstyle ':completion:*' menu select zstyle ':completion:*' matcher-list 'm:{a-zA-Z}={A-Za-z}' # LS_COLORS is a GNU coreutils thing and is empty by default; macOS used the # BSD LSCOLORS set above instead. Without this the list-colors zstyle expands # to one empty element and completion menus / fzf-tab render uncoloured. (( $+commands[dircolors] )) && [[ -z $LS_COLORS ]] && eval "$(dircolors -b)" zstyle ':completion:*' list-colors "${(s.:.)LS_COLORS}" # ── plugins : manual loader (replaces oh-my-zsh) ───────────────────────────── _PLUGIN_DIR="${ZDOTDIR:-$HOME/.dotfiles}/config/plugins" # async is the plugin default (it force-sets ZSH_AUTOSUGGEST_USE_ASYNC on load). ZSH_AUTOSUGGEST_STRATEGY=(history completion) # zsh array; not env-exportable export YSU_MESSAGE_POSITION=after # (zsh-completions' src/ joins fpath above, before compinit — appending it here # was too late for compinit to autoload any of its 191 completions.) () { local p # syntax-highlighting MUST be sourced last (it wraps ZLE). for p in zsh-autosuggestions/zsh-autosuggestions.zsh \ zsh-history-substring-search/zsh-history-substring-search.zsh \ fzf-tab/fzf-tab.plugin.zsh \ zsh-you-should-use/you-should-use.plugin.zsh \ zsh-syntax-highlighting/zsh-syntax-highlighting.zsh; do [[ -r "$_PLUGIN_DIR/$p" ]] && source "$_PLUGIN_DIR/$p" done } # history-substring-search keybinds (after plugin load) bindkey '^[[A' history-substring-search-up bindkey '^[[B' history-substring-search-down # ── integrations (all guarded; portable mac/linux) ─────────────────────────── (( $+commands[mise] )) && eval "$(mise activate zsh)" (( $+commands[zoxide] )) && eval "$(zoxide init zsh)" # --disable-up-arrow: keep up-arrow on history-substring-search; Ctrl-R = atuin. (( $+commands[atuin] )) && eval "$(atuin init zsh --disable-up-arrow)" # fzf keybindings + completion (brew path or system path) # fzf's key-bindings.zsh snapshots the whole `options` array and restores it # via eval in an `always` block. Without a tty `zle` cannot be re-set, which # emitted the lone startup warning `(eval):1: can't change option: zle`. # Suppress stderr ONLY in that case: the widgets must still be *defined* # everywhere (scripts and the test suite check for fzf-history-widget in a # non-pty `zsh -ic`), so skipping the source outright is not an option. # `[[ -o zle ]]` is NOT a usable guard -- it reads "on" even with no terminal. for _f in /opt/homebrew/opt/fzf/shell /usr/share/fzf /usr/share/doc/fzf/examples; do [[ -r "$_f/key-bindings.zsh" ]] || continue if [[ -t 0 ]]; then source "$_f/key-bindings.zsh"; source "$_f/completion.zsh" 2>/dev/null else { source "$_f/key-bindings.zsh"; source "$_f/completion.zsh" } 2>/dev/null fi break done [[ -r "$HOME/.fzf.zsh" ]] && source "$HOME/.fzf.zsh" unset _f # Re-assert atuin on Ctrl-R: fzf's key-bindings.zsh (sourced just above) binds # ^R in emacs/viins/vicmd, clobbering the atuin widget loaded earlier. Keymap- # specific widgets keep vi-mode intact -- `main` is aliased to viins here, and # the viins/vicmd widgets pass --keymap-mode to the atuin TUI. if (( $+widgets[atuin-search] )); then bindkey -M emacs '^R' atuin-search bindkey -M viins '^R' atuin-search-viins bindkey -M vicmd '^R' atuin-search-vicmd fi # zsh-abbr (brew on macOS, home.packages on NixOS — modules/home/tools.nix) for _abbr in /opt/homebrew/share/zsh-abbr/zsh-abbr.zsh \ "/etc/profiles/per-user/$USER/share/zsh/zsh-abbr/zsh-abbr.zsh"; do [[ -r $_abbr ]] && { source "$_abbr"; break; } done unset _abbr # deno [[ -r "$HOME/.deno/env" ]] && source "$HOME/.deno/env" # ── Emacs-style line editing inside vi insert mode ─────────────────────────── # `main` is aliased to viins, where ^A/^E default to self-insert -- so Ctrl-A # never jumped to the start of the line. Bind these into viins so vi-mode stays # intact. Applied last so they win over plugins loaded above. bindkey -M viins '^A' beginning-of-line # Ctrl-A -> start of line (e.g. prepend sudo) bindkey -M viins '^E' end-of-line # Ctrl-E -> end of line bindkey -M viins '^[[1;5D' backward-word # Ctrl-Left -> previous word bindkey -M viins '^[[1;5C' forward-word # Ctrl-Right -> next word bindkey -M viins '^[^?' backward-kill-word # Alt-Backspace -> delete previous word bindkey -M viins '^H' backward-kill-word # Ctrl-Backspace -> delete previous word bindkey -M viins '^?' backward-delete-char # keep plain Backspace deleting one char # NixDaemon supplies zsh-autopair through Home Manager. Load after the key # bindings above so Backspace also deletes empty pairs in the active insert # keymap. Auto-close (), [], {}, single/double quotes and backticks; typing the # closing delimiter skips over it. Inert on machines without this package. if [[ -r "$HOME/.zsh/autopair.zsh" ]]; then source "$HOME/.zsh/autopair.zsh" # Keep the existing Ctrl-Backspace word deletion binding in vi insert mode. bindkey -M viins '^H' backward-kill-word fi