2

I use Putty to connect to Linux (RHEL8). I start tmux (version 2.7) and do some work. Once done, I exit tmux. But the screen text in the tmux session is no longer in the Putty terminal. Is it possible to preserve the text (other than by running command `script' inside tmux)?

Alternatively, I'm OK to save all the screen text to a file and review it. Putty can do "Copy All to Clipboard". But inside a tmux session, it only copies the current screen. I could enter copy mode (^b[) and scroll up one screen at a time and copy, but it's tedious. I read https://unix.stackexchange.com/questions/630251/tmux-how-to-copy-large-bodies-of-text-to-system-clipboard but it's not clear how that works or helps.

2
  • 3
    Can I ask why you exit tmux? The whole point of tmux and similar programs is that you don't exit them when you log out. That way, the next day when you log in again, all your info is still there. If you want to keep the text, why are you using tmux?
    – terdon
    Jul 30 at 17:38
  • 1
    What about turning logging on in PuTTY? Raw logs will log the control codes controlling the screen/text positioning... Thus, you may have to manipulate the logs somewhat when want to pull out a specific screen... But you do not have many other options: Most systems treat the terminal screen as a streaming device, not a frame buffer device. So you cannot "save" a screen at an arbitrary point in time unless you take screenshots or record it as a video.
    – C. M.
    Jul 30 at 18:20
5

To @terdon's point in the comments, you can simply "detach" (default Ctrl>+B D) from the session and continue it later if you'd like. If you simply close Putty, then tmux is still running on the Linux host. You can reconnect to it when you next ssh to the host, usually with tmux attach-session (alias tmux attach).

Although I use WSL/Windows Terminal now, when I was using Putty I would set up the "Remote Command" option to just always reconnect to the existing (named) tmux session or create a new (named) one if it didn't exist (tmux new-session -AD -t main -s main).

However, when tmux closes (via exit or detach), as with any "curses"/full-screen type app that manipulates the terminal (e.g. less or vi), it's going to result in the text on screen being lost as it returns control to the previous terminal "owner" (usually the shell).

As for your request to save the full contents, yes, tmux can save the entire pane to either a buffer or to a file:

tmux capture-pane -p -S - -E - > ~/tmux-contents
  • -p sends the results to stdout instead of a tmux buffer
  • -S - starts at the first line of the history (default is the first line of visible contents only)
  • -E - ends at the last visible line

See man tmux and look for the capture-pane command for more info.

2
  • Thanks for your "tmux capture-pane ..." command. I'll remember to run that before I exit tmux. Your explanation of the tmux text being lost on exit makes good sense.
    – yong321
    Jul 30 at 19:33
  • 1
    Glad it helped. If you find yourself forgetting to run it, and lose something, you could consider a script that runs when you exit the shell and runs the command automatically (at least if a tmux session is running). Jul 30 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.