From 2fd11d6255d4eb46323c455830afc3ecf43ba662 Mon Sep 17 00:00:00 2001 From: Niels Mokkenstorm Date: Wed, 31 Dec 2025 11:35:42 +0100 Subject: [PATCH] Add OS-aware shell configs - Add .bashrc with OS detection (gitbash/wsl/linux/mac) - Update .zshrc with OS-aware conditional loading for macOS/WSL - Add OS_TYPE environment indicator to Starship prompt - Make starship and bat conditional (no errors if not installed) - Consolidate common aliases and settings across platforms --- .bashrc | 122 ++++++++++++++++++++++++++++++++++++++++++ .config/starship.toml | 6 +++ .zshrc | 110 +++++++++++++++++++++++++------------ 3 files changed, 203 insertions(+), 35 deletions(-) create mode 100644 .bashrc diff --git a/.bashrc b/.bashrc new file mode 100644 index 0000000..6c6a5ec --- /dev/null +++ b/.bashrc @@ -0,0 +1,122 @@ +# OS-aware .bashrc for both Git Bash (Windows) and WSL/Linux +# Part of dotfiles managed at ~/Developer/dots + +# Detect OS +case "$(uname -s)" in + Darwin*) OS_TYPE="mac";; + Linux*) + if grep -qi microsoft /proc/version 2>/dev/null; then + OS_TYPE="wsl" + else + OS_TYPE="linux" + fi + ;; + MINGW*|MSYS*|CYGWIN*) OS_TYPE="gitbash";; + *) OS_TYPE="unknown";; +esac + +# Export OS_TYPE for prompts +export OS_TYPE + +# If not running interactively, don't do anything +case $- in + *i*) ;; + *) return;; +esac + +# History settings +export HISTSIZE=10000 +export HISTFILESIZE=10000 +export HISTCONTROL=ignoredups:erasedups + +# Append to history file, don't overwrite +if [ "$OS_TYPE" != "gitbash" ]; then + shopt -s histappend +fi + +# Check window size after each command +if [ "$OS_TYPE" != "gitbash" ]; then + shopt -s checkwinsize +fi + +# Git aliases (all platforms) +alias gs='git status' +alias ga='git add' +alias gc='git commit' +alias gp='git push' +alias gl='git pull' +alias gd='git diff' +alias gco='git checkout' + +# Git tree alias +git config --global alias.tree "log --oneline --decorate --all --graph" + +# Convenient aliases +alias ll='ls -lah' +alias la='ls -A' +alias l='ls -CF' + +# cd up shortcuts +alias ..='cd ..' +alias ...='cd ../..' +alias ....='cd ../../..' + +# Platform-specific configuration +if [[ "$OS_TYPE" == "gitbash" ]]; then + # Git Bash / Windows specific + alias wsl='wsl.exe' + echo "Git Bash environment loaded. For full dev setup, use: wsl" + +elif [[ "$OS_TYPE" == "wsl" ]] || [[ "$OS_TYPE" == "linux" ]]; then + # WSL / Linux specific + + # Enable color support + if [ -x /usr/bin/dircolors ]; then + test -r ~/.dircolors && eval "$(dircolors -b ~/.dircolors)" || eval "$(dircolors -b)" + alias ls='ls --color=auto' + alias grep='grep --color=auto' + alias fgrep='fgrep --color=auto' + alias egrep='egrep --color=auto' + fi + + # Alert alias for long running commands + alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"' + + # Source bash aliases if exists + if [ -f ~/.bash_aliases ]; then + . ~/.bash_aliases + fi + + # Enable programmable completion + if ! shopt -oq posix; then + if [ -f /usr/share/bash-completion/bash_completion ]; then + . /usr/share/bash-completion/bash_completion + elif [ -f /etc/bash_completion ]; then + . /etc/bash_completion + fi + fi + + # SSH agent bridge (WSL specific) + if [ -f "$HOME/.agent-bridge.sh" ]; then + source "$HOME/.agent-bridge.sh" + fi + + # Neovim alias if installed + if [ -x /opt/nvim-linux-x86_64/bin/nvim ]; then + alias nvim="/opt/nvim-linux-x86_64/bin/nvim" + export PATH="/opt/nvim-linux-x86_64/bin:$PATH" + fi +fi + +# Add user bin to PATH (all platforms) +export PATH="$HOME/.local/bin:$HOME/bin:$PATH" + +# Better ls colors (Git Bash) +if [[ "$OS_TYPE" == "gitbash" ]] && command -v dircolors >/dev/null 2>&1; then + eval "$(dircolors -b)" +fi + +# Initialize Starship prompt if available +if command -v starship >/dev/null 2>&1; then + eval "$(starship init bash)" +fi diff --git a/.config/starship.toml b/.config/starship.toml index 5b638fa..9a54791 100644 --- a/.config/starship.toml +++ b/.config/starship.toml @@ -2,6 +2,7 @@ # Minimal prompt - user/folder/git/time moved to tmux status bar format = """ +$env_var\ $character""" palette = 'catppuccin_mocha' @@ -9,6 +10,11 @@ palette = 'catppuccin_mocha' [line_break] disabled = true +[env_var.OS_TYPE] +variable = 'OS_TYPE' +format = '[$env_value](bold fg:sky) ' +disabled = false + [character] disabled = false success_symbol = '[❯](bold fg:green)' diff --git a/.zshrc b/.zshrc index 5900f1b..8fd52a2 100644 --- a/.zshrc +++ b/.zshrc @@ -1,24 +1,49 @@ -# If you come from bash you might have to change your $PATH. -# export PATH=$HOME/bin:$HOME/.local/bin:/usr/local/bin:$PATH - -# Initialize Homebrew -eval "$(/opt/homebrew/bin/brew shellenv)" - -# Prefer 1Password's SSH agent if present -if [ -S "$HOME/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock" ]; then - export SSH_AUTH_SOCK="$HOME/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock" +# Detect OS and export for subshells/prompts +case "$(uname -s)" in + Darwin*) export OS_TYPE="mac";; + Linux*) + if grep -qi microsoft /proc/version 2>/dev/null; then + export OS_TYPE="wsl" + else + export OS_TYPE="linux" + fi + ;; + MINGW*|MSYS*|CYGWIN*) export OS_TYPE="windows";; + *) export OS_TYPE="unknown";; +esac + +# Ensure user bin directories are in PATH +export PATH="$HOME/.local/bin:$HOME/bin:/usr/local/bin:$PATH" + +# macOS-specific configuration +if [[ "$OS_TYPE" == "mac" ]]; then + # Initialize Homebrew + if [[ -x /opt/homebrew/bin/brew ]]; then + eval "$(/opt/homebrew/bin/brew shellenv)" + fi + + # Prefer 1Password's SSH agent if present + if [ -S "$HOME/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock" ]; then + export SSH_AUTH_SOCK="$HOME/Library/Group Containers/2BUA8C4S2C.com.1password/t/agent.sock" + fi fi git config --global alias.tree "log --oneline --decorate --all --graph" -alias cat='bat' +# Use bat if available +if command -v bat >/dev/null 2>&1 || command -v batcat >/dev/null 2>&1; then + alias cat='bat 2>/dev/null || batcat' +fi alias sw='f() { git checkout $(git branch | grep $1); };f' # Path to your Oh My Zsh installation. export ZSH="$HOME/.oh-my-zsh" -export HOMEBREW_NO_INSECURE_REDIRECT=1 +# macOS-specific environment +if [[ "$OS_TYPE" == "mac" ]]; then + export HOMEBREW_NO_INSECURE_REDIRECT=1 +fi # Set name of the theme to load --- if set to "random", it will # load a random theme each time Oh My Zsh is loaded, in which case, @@ -87,7 +112,14 @@ export HOMEBREW_NO_INSECURE_REDIRECT=1 # Custom plugins may be added to $ZSH_CUSTOM/plugins/ # Example format: plugins=(rails git textmate ruby lighthouse) # Add wisely, as too many plugins slow down shell startup. -plugins=(git colorize github jira vagrant virtualenv pip python brew macos docker) + +# Base plugins for all platforms +plugins=(git colorize github jira vagrant virtualenv pip python docker) + +# Add platform-specific plugins +if [[ "$OS_TYPE" == "mac" ]]; then + plugins+=(brew macos) +fi source $ZSH/oh-my-zsh.sh @@ -100,8 +132,10 @@ if [ -f "${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting/zsh- source "${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" fi -# Initialize Starship prompt -eval "$(starship init zsh)" +# Initialize Starship prompt if available +if command -v starship >/dev/null 2>&1; then + eval "$(starship init zsh)" +fi # User configuration @@ -131,16 +165,22 @@ eval "$(starship init zsh)" # Example aliases # alias zshconfig="mate ~/.zshrc" # alias ohmyzsh="mate ~/.oh-my-zsh" -export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH" -export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH" -# bun completions -[ -s "/Users/niels/.bun/_bun" ] && source "/Users/niels/.bun/_bun" +# macOS-specific paths and tools +if [[ "$OS_TYPE" == "mac" ]]; then + export PATH="/opt/homebrew/opt/mysql-client/bin:$PATH" + export PATH="/opt/homebrew/opt/postgresql@17/bin:$PATH" + + # bun completions (macOS) + [ -s "/Users/niels/.bun/_bun" ] && source "/Users/niels/.bun/_bun" + + # pipx (macOS) + export PATH="$PATH:/Users/niels/.local/bin" +fi -# bun +# bun (cross-platform) export BUN_INSTALL="$HOME/.bun" export PATH="$BUN_INSTALL/bin:$PATH" -export PATH="/opt/homebrew/opt/postgresql@17/bin:$PATH" # Docker color output export DOCKER_BUILDKIT=1 @@ -162,19 +202,19 @@ alias ds='docker stats | docker-color-output' alias dcps='docker compose ps | docker-color-output' -# BEGIN opam configuration -# This is useful if you're using opam as it adds: -# - the correct directories to the PATH -# - auto-completion for the opam binary -# This section can be safely removed at any time if needed. -[[ ! -r '/Users/niels/.opam/opam-init/init.zsh' ]] || source '/Users/niels/.opam/opam-init/init.zsh' > /dev/null 2> /dev/null -# END opam configuration - -# Created by `pipx` on 2025-12-29 18:40:28 -export PATH="$PATH:/Users/niels/.local/bin" - -# Auto-start tmux in Alacritty only (not SSH, not already in tmux) -if [[ "$TERM" == "alacritty" ]] && [[ -z "$TMUX" ]] && [[ -z "$SSH_CONNECTION" ]]; then - # Attach to existing session or create new one named 'main' - tmux attach-session -t main 2>/dev/null || tmux new-session -s main +# macOS-specific: opam configuration +if [[ "$OS_TYPE" == "mac" ]]; then + # BEGIN opam configuration + # This is useful if you're using opam as it adds: + # - the correct directories to the PATH + # - auto-completion for the opam binary + # This section can be safely removed at any time if needed. + [[ ! -r '/Users/niels/.opam/opam-init/init.zsh' ]] || source '/Users/niels/.opam/opam-init/init.zsh' > /dev/null 2> /dev/null + # END opam configuration + + # Auto-start tmux in Alacritty only (not SSH, not already in tmux) + if [[ "$TERM" == "alacritty" ]] && [[ -z "$TMUX" ]] && [[ -z "$SSH_CONNECTION" ]]; then + # Attach to existing session or create new one named 'main' + tmux attach-session -t main 2>/dev/null || tmux new-session -s main + fi fi -- 2.51.2