How to use tmux control mode?

Quick Answer

Tmux control mode allows programmatic interaction with tmux through a more machine-friendly interface. Enable it by using tmux -C to start a new session or tmux -C attach to attach to an existing one. In control mode, tmux sends event notifications and accepts commands through stdin/stdout, making it ideal for automation scripts and external tools.

tmux -C attach

Detailed Explanation

Control mode is a special interface in tmux designed for programmatic interaction rather than human interaction. It provides a way for scripts, tools, and other programs to communicate with tmux in a more structured format.

Starting Control Mode

# Start a new session in control mode
tmux -C

# Attach to an existing session in control mode
tmux -C attach

# Attach to a specific session in control mode
tmux -C attach -t session_name

When you enter control mode, tmux will output its status in a machine-readable format and wait for commands on stdin. Your terminal will appear different than normal tmux, as it's now in a command/response mode rather than the usual interactive mode.

Control Mode Output Format

In control mode, tmux outputs information in a structured format:

%begin 200 16 1
%end 200 16 1
%window-add @0
%session-changed $3 0
%layout-change @0 272 80 0 0
%output %1 some text from a pane
...

Each line begins with a % character followed by an event type. Events include session changes, window creation, pane output, and more. These events help external programs track the state of tmux.

Sending Commands in Control Mode

You can send commands to tmux in control mode by typing them directly:

new-window
split-window -h
send-keys -t %1 "echo Hello World" Enter

These commands work just like they would in the tmux command prompt, but they're processed directly from stdin. Commands don't need to be prefixed with a colon or anything else.

Using Control Mode in Scripts

Control mode is particularly useful for scripting and automation. Here's a simple example of a bash script that interacts with tmux through control mode:

#!/bin/bash

# Start tmux in control mode and send commands
{
  sleep 1
  echo "new-window -n 'script'"
  sleep 0.5
  echo "send-keys 'echo This window was created by a script' Enter"
  sleep 0.5
  echo "split-window -h"
  sleep 0.5
  echo "send-keys 'echo This is the right pane' Enter"
} | tmux -C

This script opens a new tmux session in control mode and sends commands to create a new window, run a command in it, split the window, and run another command in the new pane.

Practical Applications

Control mode enables several advanced use cases:

  • Creating complex tmux layouts programmatically
  • Building GUI interfaces that control tmux (like iTerm2's tmux integration)
  • Automated testing of terminal applications
  • Monitoring and logging tmux sessions
  • Creating interactive tmux demos

Pro Tip

For more robust programmatic control, combine control mode with Python using the libtmux library:

pip install libtmux

# Example Python script using libtmux
import libtmux
server = libtmux.Server()
session = server.new_session('python-controlled')
window = session.new_window(window_name='example')
pane = window.split_window(vertical=False)
pane.send_keys('echo "Hello from Python!"')

This provides a more structured API than raw control mode, making it easier to build complex tmux automation.

Exit Control Mode

To exit control mode, you can type exit or detach, or use the standard key sequence Ctrl+d. The tmux session will continue running in the background unless explicitly killed.

Control mode is mostly meant for machine interaction, so you'll typically want to return to the normal interactive mode for day-to-day tmux usage.