How to capture pane content in tmux?

Quick Answer

To capture pane content in tmux, use the capture-pane command followed by save-buffer to save it to a file. Run Ctrl+b : capture-pane -S - to capture the entire scrollback buffer, then Ctrl+b : save-buffer filename.txt to save it to a file. Alternatively, use tmux capture-pane -t session:window.pane -S - | tmux save-buffer - > output.txt from your shell.

# Capture entire pane with scrollback and save to file
Ctrl+b :capture-pane -S -
Ctrl+b :save-buffer filename.txt

Detailed Explanation

Capturing pane content in tmux allows you to save terminal output to a file for documentation, logging, or sharing. This is particularly useful for preserving command output, logs, or any text displayed in a tmux pane.

Basic Capture Process

Capturing pane content is a two-step process:

  1. First, capture the pane content to tmux's paste buffer using capture-pane
  2. Then, save the buffer to a file using save-buffer

Capturing Current Pane Content

To capture just the visible content in the current pane:

# In tmux command mode (Ctrl+b :)
capture-pane
save-buffer ~/visible-content.txt

Capturing with Scrollback

To capture the entire scrollback buffer (not just what's visible):

# In tmux command mode (Ctrl+b :)
capture-pane -S -  # -S - means start from the beginning of history
save-buffer ~/full-history.txt

The -S flag sets the starting line for capture. Using -S - tells tmux to start from the very beginning of the scrollback buffer.

Capturing a Specific Range

To capture a specific range of lines:

# In tmux command mode (Ctrl+b :)
capture-pane -S -50 -E -10  # Capture from 50 lines up to 10 lines up
save-buffer ~/range.txt

Here, -S -50 starts the capture 50 lines above the visible area, and -E -10 ends it 10 lines above. Negative numbers count backward from the current position.

Capturing from a Specific Pane

To capture content from a pane other than the current one:

# In tmux command mode (Ctrl+b :)
capture-pane -t session:window.pane -S -
save-buffer ~/other-pane.txt

The -t flag specifies the target pane. You can identify panes using session, window, and pane identifiers (e.g., mysession:2.1 for pane 1 of window 2 in session "mysession").

One-Line Shell Command

You can also perform the entire process from the shell (outside tmux) in one command:

# From your shell (not in tmux command mode)
tmux capture-pane -S - && tmux save-buffer ~/output.txt

# Or for a specific pane
tmux capture-pane -t mysession:0.1 -S - && tmux save-buffer ~/output.txt

# Pipe directly to a file (more efficient)
tmux capture-pane -p -S - > ~/output.txt

# With filtering
tmux capture-pane -p -S - | grep "error" > ~/errors.txt

The -p flag outputs the captured content to stdout, allowing you to pipe it directly to files or other commands.

Additional Options

The capture-pane command has several useful options:

  • -a - Include alternate screen content (useful for programs like vim)
  • -e - Include escape sequences (preserves colors and formatting)
  • -J - Join wrapped lines
  • -N - Include trailing spaces at end of lines
  • -P - Capture the pending (not yet displayed) output

Clipboard Integration

Send captured content directly to your system clipboard:

# macOS
tmux capture-pane -p -S - | pbcopy

# Linux with xclip
tmux capture-pane -p -S - | xclip -selection clipboard

# Windows with WSL
tmux capture-pane -p -S - | clip.exe

This allows you to quickly capture and paste terminal content elsewhere without creating intermediate files.