
Zsh Conda Environment Selector Function
Managing Python environments can sometimes feel like navigating a jungle π΄π, especially when dealing with dependency conflicts. To streamline this process, I created a handy Zsh function to add to your dotfile for switching Conda environments quickly and efficiently. π Let me walk you through it!
Why I Needed This Function π οΈ
After months of using Conda to manage Python environments on my macOS machines, I realized I had built up quite a collection. Switching between them manually became tedious. So, I decided to create a quick, interactive function to simplify the process. π―
How It Works π§
Hereβs the magic in action β¨:
# Conda Select function with colors and stylingfunction cs() { # Colors and formatting local BLUE='\033[0;34m' local GREEN='\033[0;32m' local YELLOW='\033[1;33m' local CYAN='\033[0;36m' local BOLD='\033[1m' local NC='\033[0m' # No Color
# Get list of conda environments local environments=($(conda env list | grep -v '^#' | awk '{print $1}' | grep -v '^$'))
# Print header with styling echo "\n${BOLD}${BLUE}π Available Conda Environments:${NC}\n"
# Print environments with numbers and colors for i in {1..${#environments[@]}}; do if [ "${environments[$i]}" = "base" ]; then echo " ${YELLOW}$i)${NC} ${CYAN}${environments[$i]}${NC} ${GREEN}(base)${NC}" else echo " ${YELLOW}$i)${NC} ${CYAN}${environments[$i]}${NC}" fi done
# Get user selection with styled prompt echo "\n${BOLD}${BLUE}Enter environment number (${GREEN}1-${#environments[@]}${BLUE}):${NC} " read selection
# Validate input if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#environments[@]}" ]; then echo "${GREEN}β Activating ${CYAN}${environments[$selection]}${GREEN} environment...${NC}" conda activate "${environments[$selection]}" else echo "${YELLOW}β οΈ Invalid selection${NC}" fi}Using the Function β‘
Running the cs command brings up a list of your Conda environments with some stylish colors and formatting. π¨ All you have to do is pick a number, and the script takes care of the rest!
And there you have it! A quick and stylish way to manage your Python environments in Zsh π. Have fun coding and keep your workflows clean and efficient! πβ¨
Share
Related Posts

Personal Project Updates and AI Editors
About that time I wrote and published an App to the Apple App Store without knowing how to code

How to Install Ansible on a Mac: A Modern Approach
A guide to installing and managing Ansible on macOS using Conda, with tips for handling collections and dependencies.

Unlocking Image Creation with Flux and GPT-4o
Explore the world of AI image generation using Flux, fal.ai, and OpenAI. Learn how to build a Streamlit app that leverages GPT-4o for prompt tuning and Flux models for creating stunning visuals. Compare outputs with Midjourney and discover the potential of these cutting-edge tools.
