Skip to main content

Tmux Scripts and Layouts

Scripted tmux layouts eliminate the repetitive task of setting up your workspace every time. Run one script and your entire environment is ready in seconds.

Learning Focus

Write scripts that are idempotent: if the session already exists, attach to it; if not, create it fresh.

Base Pattern: Idempotent Script

~/bin/work.sh
#!/bin/bash
SESSION="work"

# If session already exists, attach and exit
tmux has-session -t "$SESSION" 2>/dev/null && {
tmux attach -t "$SESSION"
exit 0
}

# Create new session (detached, initial window named "editor")
tmux new-session -d -s "$SESSION" -n "editor"

# Window 1: server
tmux new-window -t "$SESSION" -n "server"
tmux send-keys -t "$SESSION:server" "cd /srv/myapp" Enter

# Window 2: git
tmux new-window -t "$SESSION" -n "git"
tmux send-keys -t "$SESSION:git" "git status" Enter

# Window 3: logs
tmux new-window -t "$SESSION" -n "logs"
tmux split-window -v -t "$SESSION:logs"
tmux send-keys -t "$SESSION:logs.0" "tail -f /var/log/syslog" Enter
tmux send-keys -t "$SESSION:logs.1" "journalctl -f" Enter

# Focus the editor window
tmux select-window -t "$SESSION:editor"

# Attach
tmux attach -t "$SESSION"

Make it executable:

chmod +x ~/bin/work.sh

Sending Commands to Panes

# send a command to a specific pane
tmux send-keys -t session:window.pane "command here" Enter

# Examples
tmux send-keys -t work:logs.0 "tail -f /var/log/app.log" Enter
tmux send-keys -t work:editor "vim ." Enter
tmux send-keys -t work:0.1 "htop" Enter

The -t target syntax is: session:window.pane

  • window can be name or index
  • pane is 0-based index

Monitoring Dashboard Script

~/bin/monitor.sh
#!/bin/bash
SESSION="monitoring"

tmux has-session -t "$SESSION" 2>/dev/null && {
tmux attach -t "$SESSION"
exit 0
}

# Create session
tmux new -s "$SESSION" -n "system" -d

# Split into 4 panes: 2x2 grid
tmux split-window -h -t "$SESSION:system"
tmux split-window -v -t "$SESSION:system.0"
tmux split-window -v -t "$SESSION:system.1"

# Apply tiled layout
tmux select-layout -t "$SESSION:system" tiled

# Send commands to each pane
tmux send-keys -t "$SESSION:system.0" "htop" Enter
tmux send-keys -t "$SESSION:system.1" "tail -f /var/log/syslog" Enter
tmux send-keys -t "$SESSION:system.2" "watch -n 5 df -h" Enter
tmux send-keys -t "$SESSION:system.3" "watch -n 5 free -h" Enter

tmux attach -t "$SESSION"

Deployment Script

~/bin/deploy-session.sh
#!/bin/bash
SESSION="deploy"
APP_DIR="/srv/myapp"

tmux has-session -t "$SESSION" 2>/dev/null && {
tmux attach -t "$SESSION"
exit 0
}

tmux new -s "$SESSION" -n "deploy" -d

# Side-by-side: deploy + rollback
tmux split-window -h -t "$SESSION:deploy"

# Bottom row: verification
tmux split-window -v -t "$SESSION:deploy.0"
tmux split-window -v -t "$SESSION:deploy.1"

tmux select-layout -t "$SESSION:deploy" main-vertical

# Pane assignments
tmux send-keys -t "$SESSION:deploy.0" "cd $APP_DIR && echo 'Ready for deploy'" Enter
tmux send-keys -t "$SESSION:deploy.1" "echo 'Rollback commands here'" Enter
tmux send-keys -t "$SESSION:deploy.2" "watch -n 5 systemctl status myapp.service" Enter
tmux send-keys -t "$SESSION:deploy.3" "tail -f $APP_DIR/logs/app.log" Enter

tmux select-pane -t "$SESSION:deploy.0"
tmux attach -t "$SESSION"

Alias for Quick Setup

~/.bashrc or ~/.zshrc
alias work="~/bin/work.sh"
alias monitor="~/bin/monitor.sh"
alias deploy-env="~/bin/deploy-session.sh"

Common Pitfalls

PitfallSymptomFix
Script not idempotentCreates duplicate sessionsAlways check tmux has-session first
Wrong pane indexCommands go to wrong paneUse tmux list-panes -t session:window to verify
Pane content not ready before splitTiming issuesAdd small sleep between split and send-keys if needed
Missing Enter in send-keysCommand typed but not executedAlways append Enter after the command string

What's Next