How to store the contents of tmux pane

Store contents of tmux pane to a file to keep the history or inspect executed commands at some point in the future.

Capture contents using terminal commands

Capture contents of the current pane to the capture-buffer.

$ tmux capture-pane -b temp-capture-buffer -S -

Store contents of the capture-buffer buffer to the ~/tmux.log file.

$ tmux save-buffer -b temp-capture-buffer ~/tmux.log

Delete capture-buffer buffer.

$ tmux delete-buffer -b capture-buffer

Capture contents using tmux command

Define prefix-S command to capture contents to ~/tmux.log.

bind-key S capture-pane -b temp-capture-buffer -S - \; save-buffer -b temp-capture-buffer ~/tmux.log \; delete-buffer -b capture-buffer

Define prefix-Meta-S command to capture contents to specific file.

bind-key M-S command-prompt -I "~/tmux.log" -p "Store current pane contents to file:" "capture-pane -b temp-capture-buffer -S -; save-buffer -b temp-capture-buffer %1; delete-buffer -b capture-buffer"

Store these commands inside ~/.tmux.conf configuration file.

Reload tmux environment.

$ tmux source-file ~/.tmux.conf

Capture contents using tmux plugin

The third method is to use tmux-logging an easy logging and screen capturing for Tmux.

Clone tmux-plugins/tmux-logging repository.

$ git clone https://github.com/tmux-plugins/tmux-logging ~/.tmux/tmux-logging

Include downloaded plugin in your ~/.tmux.conf configuration.

run-shell ~/.tmux/tmux-logging/logging.tmux

Reload tmux environment.

$ tmux source-file ~/.tmux.conf

Use prefix + alt + shift + p to capture the current pane contents.