Write all tmux scrollback to a file

For those looking for a simple answer, just use prefix + :, then type in capture-pane -S -3000 + return (Replace 3000 with however many lines you'd like to save.) This copies those lines into a buffer.

Then, to save the buffer to a file, just use prefix + : again, and type in save-buffer filename.txt + return, replacing filename with whatever you'd like.

(By default prefix is ctrl + b.)


With tmux 1.5, the capture-pane command accepts -S and -E to specify the start and end lines of the capture; negative values can be used to specify lines from the history. Once you have the data in a buffer, you can save it with save-buffer.

Here is an example binding (suitable for .tmux.conf) that wraps it all up with a prompt for the filename:

bind-key P command-prompt -p 'save history to filename:' -I '~/tmux.history' 'capture-pane -S -32768 ; save-buffer %1 ; delete-buffer'

This captures (up to) 32768 lines of history plus the currently displayed lines. Starting with tmux 1.6, you can use numbers down to INT_MIN if your pane has a history that is deeper than 32Ki lines (usually up to 2Gi lines). Starting in tmux 2.0, you can use capture-pane -S - to mean “start at the beginning of history” (i.e. no large, hard-coded negative number).


Note: The number of lines in the saved file will not always be equal to the pane’s history limit plus its height.

When a pane’s history buffer is full, tmux discards the oldest 10% of the lines instead of discarding just one line. This means a pane’s effective history depth will sometimes be as low as 90% of its configured limit.


This depends on the value of history-limit that you have set in your .tmux.conf - the default is 2000; if you wish to capture more, you will need to explicitly set the number of lines.

To capture the entire scrollback, enter copy mode, select the entire scrollback, and yank it into the buffer, then paste it into your file.

How you accomplish this will depend on the mode-keys option you prefer, vi or emacs. man tmux has a helpful table describing the respective keys.

I have the following in my .tmux.conf to simplify this:

unbind [
bind Escape copy-mode
unbind p
bind p paste-buffer
bind-key -t vi-copy 'v' begin-selection
bind-key -t vi-copy 'y' copy-selection

The process for capturing the full scrollback is then:

PrefixEsc : to enter copy mode

v : to begin visual selection (assuming you are already at the bottom of the screen)

gg : to capture everything in the scrollback

y : to yank it into the buffer

Prefixc : open another tmux window

vim scrollback.txt

i : enter insert mode in vim

Prefixp : paste into file

There is also an answer here describing how to copy the buffer to a temporary file using xsel that might be useful.

Tags:

Tmux