How to create tmux startup scripts?

Quick Answer

Create tmux startup scripts by writing shell scripts that use tmux new-session, new-window, split-window, and send-keys commands to set up your desired layout and execute initial commands. Save this script with a .sh extension, make it executable with chmod +x, and run it to instantly create your customized tmux environment.

./my-tmux-setup.sh

Detailed Explanation

Tmux startup scripts allow you to automate the creation of complex session layouts with multiple windows and panes, each running specific commands. This is particularly useful for development environments, server monitoring, or any workflow where you need a consistent setup.

Basic Startup Script Structure

#!/bin/bash

# Start a new tmux session named "dev"
tmux new-session -d -s dev

# Rename the first window
tmux rename-window -t dev:0 "main"

# Create a new window for editors
tmux new-window -t dev:1 -n "editor"

# Split the editor window into panes
tmux split-window -h -t dev:1
tmux split-window -v -t dev:1.0

# Send commands to each pane
tmux send-keys -t dev:1.0 "cd ~/projects" C-m
tmux send-keys -t dev:1.1 "cd ~/projects && tail -f logs/dev.log" C-m
tmux send-keys -t dev:1.2 "htop" C-m

# Select window 1 and attach to the session
tmux select-window -t dev:1
tmux attach-session -t dev

Save this script to a file (e.g., dev-setup.sh), make it executable with chmod +x dev-setup.sh, and run it with ./dev-setup.sh.

Key Script Components Explained

  • tmux new-session -d -s NAME: Creates a detached session (-d) with name NAME
  • tmux new-window -t SESSION:INDEX -n NAME: Creates a new window at specified index with name NAME
  • tmux split-window -h/-v -t TARGET: Splits the window horizontally (-h) or vertically (-v)
  • tmux send-keys -t TARGET "COMMAND" C-m: Sends a command to the specified target, C-m simulates Enter
  • tmux attach-session -t SESSION: Attaches to the specified session

The target format (SESSION:WINDOW.PANE) identifies a specific pane within a window within a session.

Advanced Example: Development Environment

#!/bin/bash

# Exit if tmux session already exists
tmux has-session -t dev 2>/dev/null
if [ $? -eq 0 ]; then
  echo "Session already exists. Attaching..."
  tmux attach -t dev
  exit 0
fi

# Create a new detached session
tmux new-session -d -s dev -n "code"

# Set up code window with editor and terminal
tmux send-keys -t dev:0 "cd ~/projects/myapp" C-m
tmux send-keys -t dev:0 "vim ." C-m
tmux split-window -v -p 30 -t dev:0
tmux send-keys -t dev:0.1 "cd ~/projects/myapp" C-m

# Create server window
tmux new-window -t dev:1 -n "server"
tmux send-keys -t dev:1 "cd ~/projects/myapp" C-m
tmux send-keys -t dev:1 "npm run dev" C-m

# Create database window
tmux new-window -t dev:2 -n "db"
tmux send-keys -t dev:2 "cd ~/projects/myapp" C-m
tmux send-keys -t dev:2 "docker-compose up db" C-m
tmux split-window -h -t dev:2
tmux send-keys -t dev:2.1 "cd ~/projects/myapp" C-m
tmux send-keys -t dev:2.1 "sleep 5 && mongosh" C-m

# Select the first window and attach
tmux select-window -t dev:0
tmux attach-session -t dev

Project-Specific Startup Scripts

You can create project-specific startup scripts and place them in your project root directories:

#!/bin/bash

# Get project name from directory
PROJECT_NAME=$(basename $(pwd))
SESSION_NAME=${PROJECT_NAME//./-}

# Check if session exists
tmux has-session -t $SESSION_NAME 2>/dev/null
if [ $? -eq 0 ]; then
  tmux attach -t $SESSION_NAME
  exit 0
fi

# Create project-specific layout
tmux new-session -d -s $SESSION_NAME -n "editor"
tmux send-keys -t $SESSION_NAME:0 "vim" C-m

# Create window for running tests
tmux new-window -t $SESSION_NAME:1 -n "tests"
tmux send-keys -t $SESSION_NAME:1 "npm test -- --watch" C-m

# Create window for git operations
tmux new-window -t $SESSION_NAME:2 -n "git"
tmux send-keys -t $SESSION_NAME:2 "git status" C-m

# Attach to the session
tmux select-window -t $SESSION_NAME:0
tmux attach -t $SESSION_NAME

Save this as tmux-start.sh in your project directory. You can create a shell alias to make it easier to run from any project folder.

Pro Tip

Create a shell alias for running your tmux setup scripts:

# Add to your ~/.bashrc or ~/.zshrc
alias dev='~/scripts/dev-tmux.sh'
alias work='~/scripts/work-tmux.sh'

Now you can simply type dev or work to start your pre-configured environments.

Using Tmuxinator

For more complex setups, consider using Tmuxinator, a tool that simplifies creating and managing tmux sessions:

# Install Tmuxinator
gem install tmuxinator

# Create a new project
tmuxinator new myproject

# Edit the YAML configuration
# Then start it with:
tmuxinator start myproject

Tmuxinator uses YAML files to define your tmux layouts, making them easier to read and maintain compared to shell scripts.