Take high quality screenshots of the terminal window

screenshotterminal

I'm writing a book and sometimes I must show some screenshots taken from my terminal window. Problem is they are not good for print, so I need to increase their quality (resolution). Is there any way to take screenshots at a higher resolution? At the moment, I'm using Ubuntu (is there any way to simulate HIdpi and take the screenshot?).

Best Answer

The best way to get print-quality output from what is actually text is to actually represent it as text, rather than as a picture. If you take an actual screenshot of the terminal emulator window, then by definition of a screenshot, you'll get a screen-resolution bitmap.

You can use the script command to make a record of everything that's output to the terminal. A nice thing about using script is that it doesn't require any cooperation from the application or from the terminal. However it does have: a downside: the resulting transcript includes the escape sequences sent by the application as-is, including cursor motion, overwrites, etc. so in the general case you need to essentially write a terminal emulator to parse its output.

You can run the application in screen, and use its hardcopy command (C-a h). This produces a file (called hardcopy.0 by default) that contains the content of the screen. This does not contain any markup (bold, color, etc.).

Xterm has a print feature, which you can invoke from the Ctrl+Mouse1 (Ctrl+left-click) menu. By default, this only prints the text, but you can tell Xterm to include escape sequences for video attributes:

xterm -xrm '*printerCommand: cat' -xrm '*printModeImmediate: 2'

You can add a menu entry or keyboard shortcut to the print function with colorAttr=2 to get this effect. By default, the currently visible screen and the saved lines are included; this can be controlled via the printOptsImmediate resource or via arguments to the print function. For example, to only get the visible part, you can use

xterm -xrm '*printerCommand: cat' -xrm '*printModeImmediate: 2' -xrm '*printOptsImmediate: 1'

The output is in a file called XTerm followed by a timestamp (you can change the prefix with the printFileImmediate resource). This contains ANSI escape sequences to change text attributes, which you can convert to whatever format you use to typeset your book (e.g. via HTML).

A perhaps more convenient approach would be to use Emacs as the terminal emulator. Install a print-to-PDF-file printer (cups-pdf, should be available in your distribution). Set it as the default printer in Emacs (use the configuration interface or put (setq printer-name "pdf") in your .emacs). Run M-x term to get a terminal emulator, and select “File” → “Postscript Print Buffer” in the menu to print. (Don't use the plain “Print Buffer”, that would produce a low-quality bitmap.)