A tmux pane can fill with build logs, test output, or remote maintenance text before the output needs to be attached to a ticket, audit note, or handoff. Saving pane scrollback from the tmux server captures the retained history directly, instead of depending on terminal emulator scrollback or a manual text selection.
The capture-pane command reads a target pane and, with the -p flag, prints the captured text to standard output so the shell can redirect it into a file. The -S - and -E - range selects everything tmux still has for that pane, from the start of pane history through the end of the visible screen.
Only retained tmux history can be saved. If older lines have already rolled out of the pane's history-limit, save the current file as evidence and increase scrollback depth or enable live output logging before repeating the long-running command.
Related: How to enable output logging in tmux
Related: How to use copy mode in tmux
Related: How to configure tmux scrollback
$ tmux list-panes -a -F '#{session_name}:#{window_index}.#{pane_index} #{pane_id} #{pane_current_command}'
work:0.0 %0 bash
The work:0.0 target names the session, window, and pane index. The pane ID is the value after the target, such as %0; replace it with the pane ID from your own output.
Related: How to select a pane in tmux
$ tmux capture-pane -t %0 -p -S - -E - > /tmp/tmux-scrollback.txt
The -t option selects the pane, -p prints the capture to standard output, -S - starts at the oldest retained history line, and -E - ends at the bottom of the visible pane.
$ cat /tmp/tmux-scrollback.txt visible-marker history-line-001 history-line-002 history-line-003 ##### snipped history-line-012
The file should contain the older lines you expected from pane history. If the file starts too late, the missing lines are no longer retained by tmux and the pane needs a larger history-limit for future runs.
Related: How to configure tmux scrollback
$ tmux capture-pane -t %0 -p -J -S - -E - > /tmp/tmux-scrollback.txt
The -J flag joins wrapped lines and is useful for long log records, commands, or URLs that should remain one logical line in the saved file.
$ tmux capture-pane -t %0 -p -e -S - -E - > /tmp/tmux-scrollback.ansi
The -e flag keeps escape sequences for color and attributes. Use the plain text file for ordinary review because it is easier to read, search, and attach.
$ cp /tmp/tmux-scrollback.txt ./tmux-scrollback.txt
Keep the original file until the copied file has been attached, archived, or reviewed.