How to enable logging in tmux?

Quick Answer

To log tmux pane output to a file, use the pipe-pane command with a shell command that redirects to a file. Press Ctrl+b : then type pipe-pane -o "cat >> ~/tmux-log.txt". This logs all output from the current pane to the file. Use pipe-pane without arguments to stop logging. For more control, use the tmux-logging plugin, which adds key bindings for logging, screen capture, and save complete history functionality.

# Start logging current pane
Ctrl+b :pipe-pane -o "cat >> ~/tmux-log.txt"

# Stop logging
Ctrl+b :pipe-pane

# Log with timestamp in filename
Ctrl+b :pipe-pane -o "cat >> ~/tmux-$(date +%Y%m%d-%H%M%S).log"

Detailed Explanation

tmux provides several ways to log the output of panes, which is useful for recording terminal sessions, capturing command outputs, and maintaining history of your work.

Options for the pipe-pane command:

  • -o - Only pipes new output, not existing content
  • -I - Also pipes input to the pane
  • Without arguments, pipe-pane stops any active logging

To log a different pane than the current one, specify the target:

# Log a specific pane by index
tmux pipe-pane -t 0:1.2 -o "cat >> ~/pane-1-2.log"

# Log a specific session:window.pane
tmux pipe-pane -t mysession:2.1 -o "cat >> ~/specific-pane.log"

Using the tmux-logging Plugin

For more advanced logging features, the tmux-logging plugin offers a comprehensive solution:

# Install with TPM (Tmux Plugin Manager)
# Add to ~/.tmux.conf
set -g @plugin 'tmux-plugins/tmux-logging'

# If using TPM, press Ctrl+b I to install

The tmux-logging plugin provides several useful key bindings:

  • Ctrl+b Shift+p - Toggle logging on/off (saves to ~/tmux-logging)
  • Ctrl+b Alt+p - Save visible text in the pane
  • Ctrl+b Alt+Shift+p - Save complete pane history
  • Ctrl+b Alt+c - Clear pane history

Logging with External Tools

For even more control, you can use pipe-pane with external tools:

# Log with timestamps before each line using 'ts' (moreutils package)
tmux pipe-pane -o "cat | ts '%Y-%m-%d %H:%M:%S' >> ~/tmux-timestamped.log"

# Log with rotated log files using 'rotatelogs' (apache2-utils package)
tmux pipe-pane -o "cat | rotatelogs -n 5 ~/tmux-log.%i.txt 10M"

# Log with compression
tmux pipe-pane -o "cat | gzip > ~/tmux-log.gz"