Welcome to the Power Users community on Codidact!
Power Users is a Q&A site for questions about the usage of computer software and hardware. We are still a small site and would like to grow, so please consider joining our community. We are looking forward to your questions and answers; they are the building blocks of a repository of knowledge we are building together.
How can I capture (with forethought) the final on-screen text of a command onto the clipboard?
As a motivating example, when I write documentation for Python code I like to be able to start up a quick REPL session, play around with the code, and then copy and paste what appears on screen into the documentation. This is straightforward enough with terminal mouse support, but I'd like to be able to set up a system that handles it automatically just using the command line.
I understand that for security reasons it is not generally possible to retroactively read the terminal's buffer from another process. And simply teeing the Python process to a file breaks in a variety of ways (the standard output and error streams can be directed to a file, but the input can't, and also the Python process' readline setup will be broken), so the next obvious step is to use a tool like script to record both input and output as they occur.
However, the resulting "typescript" playback file will also contain a header and footer, and more annoyingly it will contain (in the newest Python versions) a lot of ANSI codes for repositioning the cursor, using colour etc.
I'd like to be able to "replay" this file into some kind of buffer (and disregarding all the timing information), such that I can capture the final on-screen result (without colour is fine) and send that to a clipboard with xclip. Ideally this should either trim trailing whitespace from lines, or at least have some awareness of what whitespace was actually output from the program rather than padding out every line to the full width.
The standard scriptreplay appears only to work if a timing file is also created, which is awkward to use in a simple pipeline. There's also a question of newline handling. But most importantly, if I for example use arrow keys and edit the REPL input in-place, the replayed result is going to contain commands that Python used to echo back the changes to screen — not just the final result.
Is there an existing program for this on Linux? Or a better way to get what I want? The overall goal is:
-
Write some kind of pipeline that includes
python. -
Work through an interactive Python REPL session.
-
After quitting the REPL, automatically have the clipboard populated with the text that I see on screen that resulted from my REPL session (starting with the welcome message, if it's still on screen; limiting the output to a full screen height is okay, and not doing so is also okay).
It should also work for other commands (including non-interactive ones).
For bonus points:
-
Have the option to preserve the colour of on-screen text (deduce what colour each character would be after processing all the control codes, and re-insert minimal control codes to get the same effect.
-
Have the option to choose, ahead of time, specific row/column ranges to copy onto the clipboard.
2 answers
If you use the excellent terminal emulator foot, you can add these keybindings to foot.ini:
[key-bindings]
pipe-scrollback=[wl-copy] Control+Shift+h
pipe-command-output=[wl-copy] Control+Shift+j
pipe-scrollback pipes the entire history into wl-copy while pipe-command-output uses foot's shell integration to pipe only the output of the last command.
Other terminal emulators like ghostty, alacritty, kitty, and st have similar features.
0 comment threads
There's always ASCII Cinema where you can replay the session, including the timings, and you can just pause and copy the output of any particular part.

0 comment threads