# Ghostty + tmux Startup Automation This guide shows you how to configure Ghostty terminal to automatically start tmux sessions in specific directories when you press Cmd+T. **Series navigation:** 1. [[1. tmux Basics]] - Core concepts 2. [[2. tmux Configuration with chezmoi]] - Config management 3. [[3. TPM - Tmux Plugin Manager]] - Plugins 4. **Ghostty + tmux Startup Automation** (this article) 5. [[5. tmux Session Presets]] - Project layouts ## What Problem Does This Solve? **Without automation:** - Press Cmd+T → Generic terminal opens in ~/ - Manually cd to project directory - Manually start tmux session - Manually create your layout **With automation:** - Press Cmd+T → tmux session already attached - Already in your project directory - Ready to code immediately ## Pre-Setup Checklist ✅ Ghostty terminal installed ✅ tmux installed and configured ([[2. tmux Configuration with chezmoi]]) ✅ chezmoi managing your Ghostty config ## Method 1: Simple Directory Preset (Recommended for Beginners) This method makes new Ghostty tabs open in a specific directory with tmux attached. ### Step 1: Configure Ghostty Working Directory Add to your Ghostty config (managed by chezmoi): ```bash # Edit through chezmoi chezmoi edit "Library/Application Support/com.mitchellh.ghostty/config" ``` Add these lines: ```bash # Open new windows/tabs in specific directory working-directory = /Users/yourusername/Code/github.com/theslyprofessor/midimaze # Auto-attach to tmux (see next step) shell-integration-features = no-cursor,no-sudo,no-title ``` ### Step 2: Create Startup Script Create a script that runs when terminal opens: ```bash # Create the script touch ~/.local/bin/tmux-auto-attach chmod +x ~/.local/bin/tmux-auto-attach ``` Edit `~/.local/bin/tmux-auto-attach`: ```bash #!/bin/bash # Auto-attach to tmux session, or create default session if [ -z "$TMUX" ]; then # Check if we have a default session if tmux has-session -t default 2>/dev/null; then # Attach to existing default session exec tmux attach -t default else # Create new default session exec tmux new -s default fi else # Already in tmux, just start shell exec $SHELL fi ``` ### Step 3: Add to chezmoi ```bash # Add script to chezmoi chezmoi add ~/.local/bin/tmux-auto-attach # Commit cd ~/.local/share/chezmoi git add . git commit -m "Add Ghostty tmux auto-attach script" git push ``` ### Step 4: Configure Shell to Use Script Add to your `~/.zshrc` (or `~/.bashrc`): ```bash # Auto-attach to tmux when opening new terminal # (Only if not already in tmux and TERM_PROGRAM is Ghostty) if [ -z "$TMUX" ] && [ "$TERM_PROGRAM" = "ghostty" ]; then ~/.local/bin/tmux-auto-attach fi ``` **Add to chezmoi:** ```bash chezmoi add ~/.zshrc cd ~/.local/share/chezmoi git add . git commit -m "Add tmux auto-attach to shell rc" git push ``` ### Result Now when you: 1. Open Ghostty 2. Press Cmd+T for new tab 3. **Automatically:** Opens in midimaze directory with tmux attached! ## Method 2: Smart Directory Detection This method detects if you're in a project directory and creates/attaches to project-specific sessions. ### Create Smart Script Edit `~/.local/bin/tmux-smart-attach`: ```bash #!/bin/bash # Get current directory name PROJECT_NAME=$(basename "$PWD") # If already in tmux, do nothing if [ -n "$TMUX" ]; then exec $SHELL exit 0 fi # Check if we're in a known project directory case "$PWD" in */midimaze) SESSION="midimaze" ;; */nakultiruviluamala) SESSION="website" ;; */PhD) SESSION="phd" ;; */nnt-docs) SESSION="nnt" ;; *) # Default session for other directories SESSION="default" ;; esac # Attach to or create the session if tmux has-session -t "$SESSION" 2>/dev/null; then exec tmux attach -t "$SESSION" else exec tmux new -s "$SESSION" fi ``` Make it executable and add to chezmoi: ```bash chmod +x ~/.local/bin/tmux-smart-attach chezmoi add ~/.local/bin/tmux-smart-attach ``` Update your `.zshrc`: ```bash # Smart tmux session based on directory if [ -z "$TMUX" ] && [ "$TERM_PROGRAM" = "ghostty" ]; then ~/.local/bin/tmux-smart-attach fi ``` ### Result Now tmux sessions are automatically named based on your project! ```bash # In ~/Code/.../midimaze tmux ls # Shows: midimaze (attached) # In ~/Code/.../nakultiruviluamala tmux ls # Shows: website (attached) ``` ## Method 3: Per-Tab Custom Commands (Advanced) Ghostty supports keybindings that can run different commands. ### Configure Different Keybindings for Different Projects Edit Ghostty config: ```bash chezmoi edit "Library/Application Support/com.mitchellh.ghostty/config" ``` Add custom keybindings: ```bash # Default new tab (Cmd+T) keybind = cmd+t=new_tab:~/.local/bin/tmux-smart-attach # Midimaze tab (Cmd+Shift+M) keybind = cmd+shift+m=new_tab:~/.local/bin/start-midimaze # Website tab (Cmd+Shift+W) keybind = cmd+shift+w=new_tab:~/.local/bin/start-website # PhD tab (Cmd+Shift+P) keybind = cmd+shift+p=new_tab:~/.local/bin/start-phd ``` ### Create Project-Specific Scripts **For midimaze** (`~/.local/bin/start-midimaze`): ```bash #!/bin/bash cd ~/Code/github.com/theslyprofessor/midimaze exec tmux new -s midimaze -A # -A = attach if exists ``` **For website** (`~/.local/bin/start-website`): ```bash #!/bin/bash cd ~/Code/github.com/theslyprofessor/nakultiruviluamala exec tmux new -s website -A ``` **For PhD** (`~/.local/bin/start-phd`): ```bash #!/bin/bash cd ~/Code/github.com/theslyprofessor/PhD exec tmux new -s phd -A ``` Make them executable: ```bash chmod +x ~/.local/bin/start-* ``` Add to chezmoi: ```bash chezmoi add ~/.local/bin/start-* cd ~/.local/share/chezmoi git add . git commit -m "Add project-specific tmux launchers" git push ``` ### Result Now you have keyboard shortcuts for each project: - **Cmd+T** → Smart session based on current directory - **Cmd+Shift+M** → Midimaze project - **Cmd+Shift+W** → Website project - **Cmd+Shift+P** → PhD project ## Recommended Setup for Your Workflow Based on your setup (chezmoi + midimaze focus), I recommend: ### 1. Update Ghostty Config ```bash chezmoi edit "Library/Application Support/com.mitchellh.ghostty/config" ``` Add: ```bash # Theme theme = Blue Matrix window-save-state = always # Default directory for new tabs working-directory = /Users/ntiruviluamala/Code/github.com/theslyprofessor/midimaze # Keybindings for splits keybind = cmd+d=new_split:right keybind = cmd+shift+d=new_split:down # Navigate between panes (vim-style) keybind = ctrl+h=goto_split:left keybind = ctrl+j=goto_split:bottom keybind = ctrl+k=goto_split:top keybind = ctrl+l=goto_split:right # Project shortcuts keybind = cmd+shift+m=new_tab:~/.local/bin/start-midimaze keybind = cmd+shift+w=new_tab:~/.local/bin/start-website ``` ### 2. Create tmux Auto-Attach Script ```bash # Create ~/.local/bin/tmux-auto-attach as shown in Method 1 # Add to chezmoi chezmoi add ~/.local/bin/tmux-auto-attach ``` ### 3. Update .zshrc ```bash chezmoi edit ~/.zshrc ``` Add: ```bash # Auto-attach to tmux in Ghostty if [ -z "$TMUX" ] && [ "$TERM_PROGRAM" = "ghostty" ]; then ~/.local/bin/tmux-auto-attach fi ``` ### 4. Commit Everything ```bash cd ~/.local/share/chezmoi git add . git commit -m "Configure Ghostty + tmux auto-start workflow" git push ``` Now when you press **Cmd+T**: 1. ✅ Opens in midimaze directory 2. ✅ Attaches to tmux session 3. ✅ All config is version controlled with chezmoi 4. ✅ Works on all your machines ## Troubleshooting ### Terminal opens but tmux doesn't start Check if script is executable: ```bash ls -la ~/.local/bin/tmux-auto-attach # Should show: -rwxr-xr-x ``` Make it executable: ```bash chmod +x ~/.local/bin/tmux-auto-attach ``` ### Script runs twice (nested tmux) Add the `TMUX` check to your script: ```bash if [ -n "$TMUX" ]; then exec $SHELL exit 0 fi ``` ### Ghostty config not applying Apply chezmoi changes: ```bash chezmoi apply ``` Restart Ghostty. ### Wrong directory opens Check Ghostty config: ```bash chezmoi cat "Library/Application Support/com.mitchellh.ghostty/config" | grep working-directory ``` Update if needed: ```bash chezmoi edit "Library/Application Support/com.mitchellh.ghostty/config" ``` ## Next Steps - [[5. tmux Session Presets]] - Create complex multi-window layouts for each project - [[3. TPM - Tmux Plugin Manager]] - Add plugins like tmux-resurrect to save layouts automatically - [[1. tmux Basics]] - Review core tmux concepts **Related:** - [[2. tmux Configuration with chezmoi]] - Managing tmux config - [[Dev Environment Stack]] - Understanding configuration layers