From 3585bbfb5a109f6dea31173c404f5eb3289e6f35 Mon Sep 17 00:00:00 2001 From: gwbeip Date: Sun, 25 Jan 2026 11:55:19 +0800 Subject: [PATCH] python alias and fzf alias --- zsh/alias/fzf_alias.sh | 11 +++++ zsh/alias/python_alias.sh | 84 +++++++++++++++++++++++++++++++++++++-- zsh/zshrc | 10 +---- 3 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 zsh/alias/fzf_alias.sh diff --git a/zsh/alias/fzf_alias.sh b/zsh/alias/fzf_alias.sh new file mode 100644 index 0000000..20f6a99 --- /dev/null +++ b/zsh/alias/fzf_alias.sh @@ -0,0 +1,11 @@ +eval "$(fzf --zsh)" + +if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then + if (( $+commands[fd] )); then + export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git' + elif (( $+commands[rg] )); then + export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"' + elif (( $+commands[ag] )); then + export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git' + fi +fi diff --git a/zsh/alias/python_alias.sh b/zsh/alias/python_alias.sh index 295022d..874fe3c 100644 --- a/zsh/alias/python_alias.sh +++ b/zsh/alias/python_alias.sh @@ -1,6 +1,4 @@ -if (( $+commands[python3] )); then - alias py='python3' -fi +alias py='python3' function pyclean() { find "${@:-.}" -type f -name "*.py[co]" -delete @@ -14,3 +12,83 @@ alias pygrep='grep -nr --include="*.py"' # Share local directory as a HTTP server alias pyserver="python3 -m http.server" + + +# Functions to python virtual environment ----------- +export ZSH_POLYJUICE_DEFAULT_PYTHON_VENV=$HOME/.default_pyvenv/.venv +# create or sync the default python virtual environment +function py_create_default_venv() { + if (( ! $+commands[uv] )); then + echo "❌ uv command not found. Please install uv first." + return 1 + fi + if [[ -d "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV" && -f "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV/bin/activate" ]]; then + cd $(dirname "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV") + uv sync + cd - + else + cd $(dirname "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV") || { + mkdir -p "$(dirname "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV")" + cd "$(dirname "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV")" + } + uv init --name gvenv + uv sync + cd - + fi +} + +# activate a python virtual environment +function py_vrun() { + local current_dir="$PWD" + local venv_names=(".venv" "venv" ".virtualenv" "env") + local venv_path="" + local found_name="" + local max_depth=20 + local depth=0 + + echo "🔍 searching nearest python virtual environment..." + + while [[ "$current_dir" != "/" && "$current_dir" != "$HOME" && "$depth" -lt "$max_depth" ]]; do + for venv_name in "${venv_names[@]}"; do + if [[ -d "$current_dir/$venv_name" && -f "$current_dir/$venv_name/bin/activate" ]]; then + venv_path="$current_dir/$venv_name" + found_name="$venv_name" + break 2 # break 2 levels of loops + fi + done + current_dir=$(dirname "$current_dir") + ((depth++)) + done + + # if not found in parent dirs, check $ZSH_POLYJUICE_DEFAULT_PYTHON_VENV + if [[ -z "$venv_path" && -d "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV" && -f "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV/bin/activate" ]]; then + venv_path="$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV" + found_name=$(basename "$ZSH_POLYJUICE_DEFAULT_PYTHON_VENV") + fi + + # activate the found virtual environment + if [[ -n "$venv_path" ]]; then + # 如果已经激活了虚拟环境,询问是否切换 + if [[ -n "$VIRTUAL_ENV" ]]; then + if [[ "$VIRTUAL_ENV" == "$venv_path" ]]; then + echo "✅ already activated: $venv_path" + return 0 + fi + + echo "⚠️ another virtual environment is already activated: $(basename "$VIRTUAL_ENV")" + read -p "Switch to $(basename "$(dirname "$venv_path")")/$found_name? (y/N): " -n 1 -r + echo + if [[ ! $REPLY =~ ^[Yy]$ ]]; then + return 0 + fi + deactivate 2>/dev/null || true + fi + + echo " ⚡ activating virtual environment: $venv_path/bin/activate" + source "$venv_path/bin/activate" + return 0 + else + echo "❌ no python virtual environment found." + return 1 + fi +} diff --git a/zsh/zshrc b/zsh/zshrc index c93fbcc..1ce40a9 100644 --- a/zsh/zshrc +++ b/zsh/zshrc @@ -48,16 +48,10 @@ bindkey '^[l' autosuggest-accept # alt-L to accept autosuggestion source $ZSH_POLYJUICE_PLUGINS_PATH/zsh-autocomplete/zsh-autocomplete.plugin.zsh source $ZSH_POLYJUICE_PLUGINS_PATH/zsh-syntax-highlighting/zsh-syntax-highlighting.plugin.zsh source $ZSH_POLYJUICE_PLUGINS_PATH/fzf-tab/fzf-tab.plugin.zsh # relies on fzf itself and zsh-autosuggestions plugin -source $ZSH_POLYJUICE_PLUGINS_PATH/ohmyzsh/plugins/fzf/fzf.plugin.zsh -source $ZSH_POLYJUICE_PLUGINS_PATH/ohmyzsh/plugins/zoxide/zoxide.plugin.zsh - -# PYTHON_AUTO_VRUN=true -# source $ZSH_POLYJUICE_PLUGINS_PATH/ohmyzsh/plugins/python/python.plugin.zsh - # using starship as prompt if (( $+commands[starship] )); then - eval "$(starship init zsh)" + eval "$(starship init zsh)" else - echo '[starship] not found, please install it.' + echo '[starship] not found, please install it.' fi