diff --git a/tmux/.tmux.conf b/tmux/.tmux.conf index 60ebfb1..a9db29b 100644 --- a/tmux/.tmux.conf +++ b/tmux/.tmux.conf @@ -27,8 +27,19 @@ bind -T off F12 \ setw -g mode-keys vi set-option -g status-keys vi -bind | split-window -h -c '#{pane_current_path}' -bind - split-window -v -c '#{pane_current_path}' +# Splits keep the source pane's cwd AND its active Python venv. #{@venv} is set +# per-pane by the shell (see .virtual_env_config.zsh); we go through run-shell +# because tmux expands FORMATS in a run-shell command but NOT in split-window's +# own -e value. The new pane inherits VIRTUAL_ENV and .zshrc re-activates it; +# when the source pane has no venv we omit -e so no empty var leaks in. New +# *windows* deliberately don't inherit it. +bind | run-shell 'v="#{@venv}"; d="#{pane_current_path}"; if [ -n "$v" ]; then tmux split-window -h -c "$d" -e VIRTUAL_ENV="$v"; else tmux split-window -h -c "$d"; fi' +bind - run-shell 'v="#{@venv}"; d="#{pane_current_path}"; if [ -n "$v" ]; then tmux split-window -v -c "$d" -e VIRTUAL_ENV="$v"; else tmux split-window -v -c "$d"; fi' + +# New windows always start in the base environment: -e clears any inherited +# VIRTUAL_ENV (e.g. if the tmux server was first started from inside a venv, so +# its global env carries one), regardless of what the source pane had. +bind c new-window -e VIRTUAL_ENV= # Switch windows bind-key -n M-Left select-window -t -1 diff --git a/zsh/.mac_config.zsh_mac.example b/zsh/.mac_config.zsh_mac.example index 8880c93..74ca4ac 100644 --- a/zsh/.mac_config.zsh_mac.example +++ b/zsh/.mac_config.zsh_mac.example @@ -30,3 +30,5 @@ fi export DYLD_FALLBACK_LIBRARY_PATH="$HOMEBREW_PREFIX/lib:${DYLD_FALLBACK_LIBRARY_PATH}" export GI_TYPELIB_PATH="$HOMEBREW_PREFIX/lib/girepository-1.0:${GI_TYPELIB_PATH}" export PKG_CONFIG_PATH="$HOMEBREW_PREFIX/lib/pkgconfig:$HOMEBREW_PREFIX/share/pkgconfig:${PKG_CONFIG_PATH}" +alias code="workon claude; claude" +alias python="python3" diff --git a/zsh/.virtual_env_config.zsh_mac.example b/zsh/.virtual_env_config.zsh_mac.example index 93987e0..c542367 100644 --- a/zsh/.virtual_env_config.zsh_mac.example +++ b/zsh/.virtual_env_config.zsh_mac.example @@ -10,6 +10,20 @@ export VIRTUALENVWRAPPER_VIRTUALENV=$PREFIX/bin/virtualenv mkdir -p $WORKON_HOME source $PREFIX/bin/virtualenvwrapper.sh -if [ -n "$VIRTUAL_ENV" ] && [[ -f "$VIRTUAL_ENV/bin/activate" ]]; then - source $VIRTUAL_ENV/bin/activate; -fi +# NOTE: an inherited VIRTUAL_ENV is *not* re-activated here — that happens at the +# very end of .zshrc so activate captures the fully-built PATH (see the comment +# there). Sourcing virtualenvwrapper.sh above only defines workon/deactivate/etc. + +# Mirror the current venv into a per-pane tmux option so that splitting a pane +# can carry it into the new pane (.tmux.conf reads #{@venv} with `-e`). Per-pane +# (not session) state is what lets two panes hold different venvs and lets new +# *windows* start clean. Only fork tmux when the value actually changes. +_venv_track_precmd() { + [[ -n $TMUX ]] || return + if [[ "${VIRTUAL_ENV:-}" != "${_LAST_TRACKED_VENV-unset}" ]]; then + tmux set -p @venv "${VIRTUAL_ENV:-}" 2>/dev/null + _LAST_TRACKED_VENV="${VIRTUAL_ENV:-}" + fi +} +autoload -Uz add-zsh-hook +add-zsh-hook precmd _venv_track_precmd diff --git a/zsh/.zshrc b/zsh/.zshrc index e561c88..7528bea 100644 --- a/zsh/.zshrc +++ b/zsh/.zshrc @@ -176,3 +176,26 @@ zstyle :bracketed-paste-magic paste-finish pastefinish ZSH_AUTOSUGGEST_CLEAR_WIDGETS+=(bracketed-paste) export PATH=$PATH:/Users/ising/.runai/bin source <(/Users/ising/.runai/bin/runai --quiet completion zsh) + +# Re-activate an inherited virtualenv — MUST be the last PATH-affecting line. +# +# When VIRTUAL_ENV is present in the environment (a tmux pane split carries it +# in via `-e VIRTUAL_ENV=...`, see .tmux.conf) we source the venv's activate +# script here rather than in .virtual_env_config.zsh. activate snapshots the +# current PATH into _OLD_VIRTUAL_PATH and restores it on `deactivate`; if we +# activated earlier (before .mac_config.zsh, go, runai below all prepend to +# PATH) that snapshot would be incomplete and `deactivate` would strip +# ~/.local/bin & friends — dropping claude and co. off PATH. Keeping this last +# guarantees the snapshot is the fully-built PATH. +# +# ANY new PATH mutation must go ABOVE this block. +# +# A new tmux window clears the var to empty (`bind c ... -e VIRTUAL_ENV=`) to +# force the base environment; drop that empty value so nothing sees a bogus venv. +[ -z "${VIRTUAL_ENV:-}" ] && unset VIRTUAL_ENV +if [ -n "$VIRTUAL_ENV" ] && [ -f "$VIRTUAL_ENV/bin/activate" ]; then + # Guard against double-activation on a manual `source ~/.zshrc`. + if [[ ":$PATH:" != *":$VIRTUAL_ENV/bin:"* ]]; then + source "$VIRTUAL_ENV/bin/activate" + fi +fi