10

Is it possible to, somehow, access xterm's scrollback buffer as a (read-only) file or a character device?

The core issue (to avoid x/y "problemming"), is this: sometimes the command I've just executed creates non-deterministic output, and I'd like to use its output somehow without pre-thought of tee-ing it. Right now, the only way to do this (that I'm aware of) is to use the mouse to select the text into primary selection.

John Z.
  • 123
  • 7

3 Answers3

10

You could do this by telling xterm to print the data using the print-everything action (normally not bound to a key).

Alternatively, there's an escape sequence documented in XTerm Control Sequences:

CSI ? Pm i                                                                      
          Media Copy (MC), DEC-specific.                                        
            Ps = 1  -> Print line containing cursor.                            
            Ps = 4  -> Turn off autoprint mode.                                 
            Ps = 5  -> Turn on autoprint mode.                                  
            Ps = 1  0  -> Print composed display, ignores DECPEX.               
            Ps = 1  1  -> Print all pages.

which could be invoked as

printf '\033[?11i'

But either approach (to write to a file) would need a printerCommand configured.

Thomas Dickey
  • 76,765
  • Is it possible to do that without restarting xterm? If it involves restarting xterm it is not really an answer as that would cause the output to be lost. – kasperd Apr 29 '18 at 17:54
  • This is what I was looking for. Resources can be reloaded on the fly, and I'm OK to restart xterm. I've created a named fifo and configured it into printerCommand, have bound print-everything command to a key and it works exactly as I wanted it to. – John Z. May 18 '18 at 18:18
  • This may have security implications because any program with the user rights or root rights can trigger printf '\033[?11i' > /dev/pts/[number] and then examine the output. – u_Ltd. Feb 18 '23 at 13:44
  • However, if this is to be configured permanently, just add this line to ~/.Xresources: xterm*VT100.printerCommand: cat >~/XTerm.pseudoprinter.out and make sure this is loaded by xrdb -load on startup (will probably happen automatically on your GNU/Linux system). – u_Ltd. Feb 18 '23 at 13:45
2

You can always use something like tmux which provides capture-pane that does what you want, along with splits (vertical and horizontal) and multiplexing, session support, and a slew of other things.

Or use a different terminal emulator xfce4-terminal is also lightweight and provides an easy Save Contents

Evan Carroll
  • 30,763
  • 48
  • 183
  • 315
  • 1
    To me this does not sound like an answer to the question. None of those programs can magically capture output which at the moment exist only in an xterm window. – kasperd Apr 29 '18 at 17:56
  • @kasperd a fair interpretation. He wants something that captures without pre-thought. I use tmux without prethought. Then I don't have this problem. If you can get in the habit of using tmux, you'll never have to think about this again (or a lot of other stuff). – Evan Carroll Apr 29 '18 at 18:10
0

You can Ctrl-Clickleft and select "Print-All Immediately" which generates the plain text file $HOME/XTermYYYY-MM-DD.hh.mm.ss

This is the default configuration which can be modified with printFileImmediate, printModeImmediate and printOptsImmediate.

u_Ltd.
  • 111