Oh My Bash is a powerful framework for managing your Bash configuration. By default, it offers various themes to personalize your terminal. But what if you want your terminal to dynamically change its theme based on the environment or directory you’re working in? This post will guide you through a script that achieves just that.

The Script: Dynamic Theme Switching

The following script dynamically updates the Oh My Bash theme based on your current environment and directory. Here’s the code:

themes_function() {
    local new_theme=""

    case "$env:$PWD" in
        devbox:*)
            new_theme="devbox"
            ;;
        *:/home/jose/gitlab/personal/*|*:/home/jose/gitlab/personal)
            new_theme="jose"
            ;;
        *:/home/jose/gitlab/customer/*|*:/home/jose/gitlab/customer)
            new_theme="customer"
            ;;
        *)
            new_theme="bakkex"  # Default theme
            ;;
    esac

    # Only change the theme if it's different from the current theme
    if [[ "$new_theme" != "$CURRENT_OSH_THEME" ]]; then
        CURRENT_OSH_THEME="$new_theme"
        export OSH_THEME="$CURRENT_OSH_THEME"
        source "$OSH"/oh-my-bash.sh
    fi
}

# Add the function to PROMPT_COMMAND
PROMPT_COMMAND="themes_function; $PROMPT_COMMAND"

How It Works

  1. Environment and Directory Matching:

    • The script uses a case statement to match combinations of the environment variable env and the current working directory (PWD).
    • Depending on the match, it assigns a specific theme to the new_theme variable.
  2. Theme Switching:

    • If the selected theme is different from the current theme (CURRENT_OSH_THEME), the script updates the theme by:
      • Setting the CURRENT_OSH_THEME and OSH_THEME variables.
      • Re-sourcing the Oh My Bash script (oh-my-bash.sh) to apply the new theme.
  3. Automatic Execution:

    • The themes_function is added to the PROMPT_COMMAND variable, ensuring it runs every time the terminal prompt is displayed.

Customizing the Script

  • Environment Variable env: Ensure the env variable reflects your actual terminal environment. If it’s not set, you can modify the script to use another variable or remove it entirely.

  • Theme Names: Replace devbox, jose, allianz, and bakkex with your preferred Oh My Bash themes. Make sure these themes are installed and available in your setup.

  • Directory Paths: Update the paths in the case statement to match the directories relevant to your workflow.

Applying the Script

  1. Add the script to your .bashrc or .bash_profile file:

    nano ~/.bashrc
    
  2. Save the file and reload your shell configuration:

    source ~/.bashrc
    
  3. Test the script by navigating to different directories and observing the theme changes.

Conclusion

With this script, you can add a dynamic and context-aware touch to your terminal setup. Oh My Bash’s flexibility combined with Bash scripting allows for powerful customizations tailored to your needs. Happy coding!