python alias and fzf alias

This commit is contained in:
2026-01-25 11:55:19 +08:00
parent f532737ed9
commit 3585bbfb5a
3 changed files with 94 additions and 11 deletions

11
zsh/alias/fzf_alias.sh Normal file
View File

@@ -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

View File

@@ -1,6 +1,4 @@
if (( $+commands[python3] )); then
alias py='python3'
fi
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
}

View File

@@ -48,12 +48,6 @@ 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