Introduction to tmux Scripting
tmux provides powerful scripting capabilities that allow you to:- Automate session and window creation
- Execute conditional logic
- Run shell commands in the background
- Display formatted information
- Load and organize configuration files
All tmux commands can be scripted, whether from configuration files, shell scripts, or key bindings.
The if-shell Command
Theif-shell command executes tmux commands conditionally based on shell command results.
Basic Syntax
Simple Conditional
Background Execution
Format Expansion
Use-F to treat the shell command as a format string instead of executing it:
Real-World Examples
- Conditional Config Loading
- Feature Detection
- Dynamic Status
Nested if-shell
You can nestif-shell commands:
The run-shell Command
Therun-shell command executes shell commands in the background without creating a window.
Basic Syntax
Simple Execution
Output Display
By default, command output is shown in view mode:Running tmux Commands
Use-C to run tmux commands instead of shell commands:
Delayed Execution
Working Directory
Practical Examples
Background Tasks
Background Tasks
Error Logging
Error Logging
Session Snapshots
Session Snapshots
The display-message Command
Thedisplay-message command shows messages and evaluates formats.
Basic Display
Format Expansion
Display formatted information:Script Integration
- Get Values
- Conditional Display
- Formatted Output
Verbose Mode and Debugging
Forwarding Input
The source-file Command
Thesource-file command loads and executes tmux commands from files.
Basic Usage
Format Expansion
Parse Without Executing
Organized Configuration
Split configuration into multiple files:Example: options.conf
Example: options.conf
Example: bindings.conf
Example: bindings.conf
Example: status.conf
Example: status.conf