How can I write all the scrollback in a tmux session to a file?
capture-panel can grab the current screen, but not the entire scrollback.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
For those looking for a simple answer:
- Use prefix + :, then type in
capture-pane -S -3000+ Return. (Replace-3000with however many lines you’d like to save, or with-for all lines.) 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.
(By default Prefix is Ctrl+B.)
Method 2
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.
Method 3
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.
Method 4
If you want something you can run from the command line (instead of using your tmux prefix keys), try running:
tmux capture-pane -pS -1000000
If you run it and it seems to not do anything, that’s because it’s outputting exactly what was just on your screen, so it looks the same.
Of course, you can also pipe it into a file:
tmux capture-pane -pS -1000000 > file.out
See the tmux man page and search for capture-pane for more things you can do (like capture escape sequences in case you want to preserve color, or specify whether you want multiple visual lines to be joined when they don’t contain a new line)
Method 5
I had standard key bindings which appeared to be a bit different than in @jasonwryan’s answer and didn’t change anything in config.
Below is recipe that worked for me. Maybe you will find it useful if you don’t want to make any changes in tmux config and just want to quickly copy some of the scrollback.
Prefix == Ctrl+b in my tmux (tmux 1.6, debian 7).
- Enter select mode: Prefix + [.
- Start selection: Space.
- Highlight necessary text using vim navigation (for instance, use arrow keys or press gg to reach beginning of output history).
- Actually copy in internal clipboard using Enter. You will be exited from copy mode.
- Open any file using vim (probably on new tmux tab) and paste content you copied before using Prefix + ].
- Then you may do cat of that file or use output how you need.
Method 6
Here’s a tmux plugin that enables this:
https://github.com/tmux-plugins/tmux-logging
After you install it, save the entire scrollback with prefix + alt-shift-p.
Method 7
This is actually very easy.
Enter the command mode by press prefix key then :.
Then do capture-pane -S -<line number you want to dump>
Then save-buffer <filepath>
That file contains all the scrollback output. You should delete the buffer afterwards for safety reason.
Method 8
How can I write all the scrollback in a tmux session to a file?
I use this in my ~/.tmux.conf, and now when I exit my running shell, pane output is saved to unique log file:
set -g remain-on-exit
set-hook pane-died 'capture-pane -S - -E - ; save-buffer "$HOME/logs/tmux/tmux-saved.#{host_short}-#{session_id}:#{window_id}:#{pane_id}-#{pane_pid}-#{client_activity}.log"; delete-buffer; kill-pane'
Method 9
Write all the scrollback in a tmux:
tmux capture-pane -pS - > file
where ‘-’ to -S is the start of the history, as the manual says.
For all panes in the session, you can loop through all the panes with tmux list-panes -s ....
Method 10
Dump to a file and automatically open the file in vim
This is sweet:
bind-key v 'capture-pane' ; capture-pane -S - ; save-buffer /tmp/tmux ; delete-buffer ; send-keys Escape 'ddivim /tmp/tmux' Enter
This solution supposes that your shell is in vi mode, so that:
- Escape goes into normal mode
ddclears any existing commandigoes into insert mode- then we run
vim /tmp/tmux
Tested in tmux 3.0.
Newline insertion on terminal wrap issue
One problem with this is that it inserts literal newlines on any line that would have been broken up due to terminal wrapping if output lines are longer than you terminal width. And we usually don’t want that.
I tried to fix this with -J as mentioned here but that caused another problem, it started adding trailing space characters to each line.
Eric Cousineau proposes a workaround for that with sed, but I’d really rather avoid this as it would remove actual newlines emitted by the commands, which I want to be there.
Kind of the inverse of this was asked at: https://github.com/tmux/tmux/issues/422 Add capture-pane option to only preserve trailing spaces. Maybe the space thins is a fundamental terminal limitation?
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0