Skip to content

Instantly share code, notes, and snippets.

@Stfort52
Created October 16, 2025 09:01
Show Gist options
  • Select an option

  • Save Stfort52/b2b53219c04641342bb22a43a8163b33 to your computer and use it in GitHub Desktop.

Select an option

Save Stfort52/b2b53219c04641342bb22a43a8163b33 to your computer and use it in GitHub Desktop.
Adds a function here to activate conda env matching your working directory and its parents. Useful if you make conda env names from project directory name.
function init_conda_envs() {
local env_dir="$HOME/path/to/your/conda/dir/envs"
if [[ -d "$env_dir" ]]; then
export CONDA_ENVS=$(ls -1 "$env_dir" | xargs)
else
export CONDA_ENVS=""
fi
}
function here() {
local dir=$(pwd)
local envs=($CONDA_ENVS)
while [[ "$dir" != "/" ]]; do
local base=$(basename "$dir")
for env in "${envs[@]}"; do
if [[ "$base" == "$env" ]]; then
echo "Activating environment: $env"
conda activate "$env"
return 0
fi
done
dir=$(dirname "$dir")
done
echo "No matching env found"
return 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment