397

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.

11 Answers11

594

For those looking for a simple answer:

  1. Use prefix + :, then type in capture-pane -S -3000 + Return. (Replace -3000 with however many lines you'd like to save, or with - for all lines.) This copies those lines into a buffer.
  2. Then, to save the buffer to a file, just use prefix + : again, and type in save-buffer filename.txt + return. (by default it'll save the file in ~/)

(By default Prefix is Ctrl+B.)

Sauce McBoss
  • 6,411
  • 46
    save-buffer filename.txt seems to save the file in /, not in pwd (current directory). Instead, I provided an absolute file path and it worked like a charm – MohamedEzz Dec 08 '16 at 09:54
  • 27
    and don't forget that MINUS in front of the <> – Yordan Georgiev Dec 23 '16 at 12:46
  • 12
    +n>1 After logging in to upvote, it would appear this is at least the second time this answer has been helpful to me. XD – L0j1k Mar 28 '19 at 03:01
  • 22
    Anything you can do via prefix+:, you can also do as a command arg to the tmux binary. So, anytime an answerer gives you a hint like capture-pane, search man tmux for it. Then you will find that -p prints to stdout. Then you can deduce that the simplest solution is tmux capture-pane -pS -1000000 > transcript.txt. I've taught you to fish. Use this for all things tmux. – Bruno Bronosky Apr 26 '19 at 16:03
  • 6
    @MohamedEzz For me, save-buffer filename.txt saved to home directory. – Jean Paul May 20 '19 at 14:16
  • @BrunoBronosky Your solution doesn't work because it opens a new tmux session so it does not capture the current panel. – Jean Paul May 20 '19 at 14:21
  • @JeanPaul running tmux inside of tmux is supposed to work to refer to the actively running instance of tmux. Your config is probably messed up. – Steven Lu Nov 27 '19 at 17:53
  • 4
    Just noting that this way of fetching the pane contents wraps lines based on the pane's current size, whereas yanking the buffer interactively with copy mode preserves the original line breaks from the terminal output. This is a pretty important distinction. – Steven Lu Nov 27 '19 at 17:54
  • 1
    @MohamedEzz, just in case save-buffer "~/filename.txt" should save to the directory of the user that Tmux is running as. – Artfaith Dec 13 '21 at 16:49
  • 2
    Note that the file is saved on the server running tmux, not on the server in the pane. It's probably obvious but I lost 5 min understanding that. – Bastien974 Jul 05 '22 at 19:09
194

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.

Chris Johnsen
  • 20,101
  • Thanks for the answer… But my version of tmux doesn't accept -S to capture-pane (it appears to be 1.3-2, although that's from dpkg, as I can't figure out how to get tmux to show me a version number…) – David Wolever Dec 12 '11 at 05:40
  • 1
    You probably are running tmux 1.3; you can probably use tmux server-info | head -1 to see your version. tmux -V works in tmux* 1.4 and later. – Chris Johnsen Dec 12 '11 at 08:03
  • 1
    And if you are already in your tmux window and don't want to restart just do a [PrefixKey] : to get to the tmux command line, and then paste the whole line, then you just do a [Prefix] P it is capital P and you are good to go. – Ali Dec 17 '13 at 21:43
  • I just want to point out that you said 32Ki and 2Gi instead of 32K and 2G, as in 32 Kibi- (2^10) / 2 Gibi- (2^30) instead of 32 Kilo- (10^3) / 2 Giga- (10^9). Nice job getting the SI prefixes correct! – Braden Best May 19 '17 at 18:20
  • Add -e to capture-pane (i.e., capture-pane -e) to include colors in Tmux 1.8 and up. – William Denniss May 25 '17 at 23:00
  • 2
    Is it possible to have the file name of the tmux buffer file generated automatically rather than prompted for? So everything you run the shortcut it would save to another file, for example with the current time in the filename. – user779159 Sep 16 '17 at 12:44
92

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)

  • 1
    The -p argument outputs to stdout instead of a tmux buffer. – palswim Dec 01 '19 at 06:24
  • 2
    If you are aren't in a local console this won't work so what I've tried is the following: 1) add a horizontal pane below 2) in the local shell, tmux capture-pane -t {top} -pS -1000 > ~/tmp.out – Adverbly Jul 27 '20 at 13:57
  • 4
    On my tmux (2.6-3ubuntu0.2), you can use -S- to capture all the available history in the pain: tmux capture-pane -p -S- > /tmp/output.txt. First-hand source (I think): https://man7.org/linux/man-pages/man1/tmux.1.html#COMMANDS, "-S and -E specify the starting and ending line numbers [...] ‘-’ to -S is the start of the history" – Eric Cousineau Mar 14 '21 at 19:21
  • 5
    Ah, also useful: Use -J so that wordwrapping in your buffer is removed when dumped to a file, caveat is training spaces, so sed can help. Full cmd: tmux capture-pane -p -J -S- | sed -E 's# +$##g' > /tmp/output.txt – Eric Cousineau Mar 24 '21 at 21:42
  • 1
    @EricCousineau do you understand why -J adds trailing spaces? That is really a shame, I wish I could avoid seding all of them out in case there are actual trailing spaces in the output. – Ciro Santilli OurBigBook.com Apr 25 '22 at 11:08
  • That's a bit strange, why :capture-pane not suffer this: nowrap, no extra tail space Oh that way face same problem – yurenchen Sep 10 '23 at 06:35
70

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.

jasonwryan
  • 73,126
  • 4
    This is problematic at best … pasting into insert mode in Vim has all kinds of problems, e.g. when you have automatic indentation enabled. I never got this to work to my satisfaction. – Konrad Rudolph Jan 21 '15 at 16:19
  • 22
    If you use :set paste in vim, vim will ignore adding automatic indentations or any insert-based keybindings. – tlunter Feb 04 '15 at 21:18
  • Can this be done with default key bindings? – daveloyall Nov 05 '15 at 17:00
  • @daveloyall Of course, just don't use the keybind options I included from my .tmux.conf... – jasonwryan Nov 05 '15 at 17:14
  • @KonradRudolph the way I got around that was by instead using cat << EOF >> file, and then pasting the tmux buffer, and then terminating it with a line containing only EOF. Worked like a charm. – Braden Best May 19 '17 at 18:15
  • @BradenBest Yeah, that's what I'm doing as well on systems where I can't access the clipboard. – Konrad Rudolph May 19 '17 at 22:56
  • For the pasting into Vim I found a solution and answered another question with a nice way to do it. https://superuser.com/a/904446/333828 – dragon788 Jun 16 '17 at 15:54
28

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).

  1. Enter select mode: Prefix + [.
  2. Start selection: Space.
  3. Highlight necessary text using vim navigation (for instance, use arrow keys or press gg to reach beginning of output history).
  4. Actually copy in internal clipboard using Enter. You will be exited from copy mode.
  5. Open any file using vim (probably on new tmux tab) and paste content you copied before using Prefix + ].
  6. Then you may do cat of that file or use output how you need.
Alex
  • 381
  • man tmux helped me to sort out that my tmux was in emacs mode, so none of the key-bindings above worked. man tmux, again, helped me sort out what to use. But the biggest mistake that I made was that the host that I saved the contents from was not the host that I was running tmux from, so I kept looking for the saved file on the wrong host ... – Cognitiaclaeves Aug 04 '17 at 01:29
20

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.

12

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 ....


How to specify a target pane?

tmux capture-pane -t :WINDOW.PANE -pS - > file

Use -t to specify target pane. The format is SESSION:WINDOW.PANE. For example, -t :1.2 means current tmux session, window 1, pane 2.

To identify the pane, use prefix q, which prints the ID for each pane for the current window.

Peregrino69
  • 2,417
tinnick
  • 300
  • 2
  • 10
9

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'                        
8

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.

Wang
  • 1,296
6

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
  • dd clears any existing command
  • i goes 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?

Ciro Santilli OurBigBook.com
  • 18,092
  • 4
  • 117
  • 102
  • 3
    +1 Wonderful answer, but writing the scrollback to a file in /tmp looks like a bad idea since others get the permission to read it! Additionally, in case one is going to use this just to navigate the scrollback with full Vim powers and don't actually want to keep it in a file polluting the filesystem, then one could immediately delete the file after Vim gets its contents in a buffer. E.g. this is what I do: bind v capture-pane -S - \; save-buffer ~/.x \; delete-buffer \; new-window 'vim "set buftype=nofile" +"!rm ~/.x" ~/.x +'. – Quasímodo Apr 29 '21 at 20:05
  • This is a decent solution but doesn't work well. I got $ divim /tmp/tmux -bash: divim: command not found. looks like sending keys ddi has issues. tmux version. 3.3a – Justin Lin Aug 16 '22 at 16:15
  • @JustinLin is your terminal in vim mode? https://unix.stackexchange.com/questions/4870/is-it-possible-to-have-vim-key-bindings-in-terminal – Ciro Santilli OurBigBook.com Aug 16 '22 at 17:48
  • @CiroSantilliПутлерКапут六四事 I have tmux.conf set-window-option -g mode-keys vi. guess that what you mean by vim mode – Justin Lin Aug 16 '22 at 18:21
  • @JustinLin no, I mean in .inputrc: https://unix.stackexchange.com/questions/4870/is-it-possible-to-have-vim-key-bindings-in-terminal/4872#4872 – Ciro Santilli OurBigBook.com Aug 17 '22 at 05:04
  • Thanks @CiroSantilliПутлерКапут六四事 let me try it out – Justin Lin Aug 17 '22 at 14:49
0

I tried the answers from here quite extensively, and ran into a repeated problem that not all scroll back was recorded due to its size, wether it just became too big or I forgot to increase the limit on a new machine. I ended up saving the log to a file on the fly:

tmux pipe-pane -o 'cat >>~/tmux.log'

and then

tmux pipe-pane

to stop logging. That circumvents all limits and also works even if machine crashes and you are unable to save scroll back manually.

If you want to lower HDD use you can use "buffer" utility. This command will write each time log increases by 16kb:

tmux pipe-pane -o 'buffer -s 1k -m 16k > ~/tmux.log'