# How to save GNOME Terminal contents with ANSI escape codes?

VT100 terminals have a relatively simple way to save terminal contents with escape codes to a file using Meta-p:

XTerm.VT100.printAttributes: 2
XTerm.VT100.printerCommand: cat - > file
XTerm.VT100.translations: #override Meta <KeyPress> P: print() \n


I've looked around quite a bit, and can't find a solution for this to work in the default GNOME Terminal + XTerm + Bash.

I do not want just an image with the contents of the window.

It looks like GNOME Terminal might eventually get a fix for this, but there's no information about which version it will be included in, and it's been almost three years since the last update...

-
Unfortunately, there is simply no way to do this. See: askubuntu.com/q/20739 –  Josh Apr 29 '13 at 15:26
@Josh I don't think that's the same question. I don't want to save all output from a command, but rather all visible output in the Terminal window. –  l0b0 Apr 30 '13 at 12:54

Unfortunately, l0b0 the answer out of the box is: This is not possible.

In my other answer, I listed how to use script to do this, though there are some differences as you noted. The other way is to compile the source or use an unstable version.

Gnome Terminal Source Tarball

The version in Raring is 3.6.1, so you can look at this source. Inside of Terminal-window.c on line 115:

/*
* We don't want to enable content saving until vte supports it async.
* So we disable this code for stable versions.
*/

#include "terminal-version.h"

#if (TERMINAL_MINOR_VERSION & 1) != 0
#define ENABLE_SAVE
#else
#undef ENABLE_SAVE
#endif
#endif


Options:

1. Add #define ENABLE_SAVE under this.
2. Modify the version to an unstable.

Hope this helps.

-
Does this save the ANSI escape keys, or just the text? –  l0b0 May 2 '13 at 19:41
I've verified and vte_terminal_write_contents() doesn't appear to write ANSI codes out, at the moment, my original response is the most accurate "What you want is not possible (out of the box)." If you are developer-minded you can probably rewrite the function in termainal-window.c 3340 save_contents_dialog_on_response(...) At about line 3372 you see where the call to vte_terminal_write_contents is made. I am not going to go further as it is beyond the scope of this site. Other terminals do offer this at far less of a cost than recompiling your utilities. –  Kaylus May 3 '13 at 3:28
+100. If possible I would split it between this and your other answer, since they both do 90% of what I want. –  l0b0 May 4 '13 at 7:56

Unfortunately, l0b0 the answer out of the box is: This is not possible.

There are two ways you can attempt to rectify this, by running script at all times on your terminal and logging all output. Open up gnome-terminal, go to Edit->Profile Preferences, then the tab Title and Command and for your custom command use this:

bash -l -c 'script -q -c bash $HOME/sessionlogs/$BASHPID.sessionlog.$(date +%Y%m%d-%H%M).txt'  Make sure 'sessionlogs' exists under$HOME.

The other way has been removed and added as a second answer on this question.

-
Indeed, corrected the answer to fix the typo which occurred during a repaste. I am separating out the 'recompile' option as well, as I agree the two are unrelated. –  Kaylus May 2 '13 at 15:34
less -r actually shows the output as printed - Thanks! –  l0b0 May 4 '13 at 7:56

You can run your shells via script(1) to get all input and output of the terminal stored in a file, including terminal escapes.

-
That's a different use case. script can't be used to save content after the fact and it doesn't simply save the visible output. –  l0b0 Aug 30 '12 at 14:57
Are you sure? True it can't save scripts after the fact, but when I tested this and output some file permissions with ll it saved all the output. –  Matt May 3 '13 at 7:23

There's a package that's called screen - a terminal multiplexor with VT100/ANSI terminal emulation.

According to the manpage of screen on the Ubuntu Manuals website , screen claims to be

a full-screen window manager that multiplexes a physical terminal between several processes (typically interactive shells).Each virtual terminal provides the functions of a DEC VT100 terminal and, in addition, several control functions from the ISO 6429 (ECMA 48, ANSI X3.64) and ISO 2022 standards (e.g. insert/delete line and support for multiple character sets) a terminal multiplexor with VT100/ANSI terminal emulation'.

and when running screen:

Ctrl ah (hardcopy) Write a hardcopy of the current window to the file "hardcopy.n".

I tried in the GNOME Terminal, running screen and typing Ctrl ah

This is the output of my hardcopy.1 file when opened in Gedit:

You can find screen is in the raring/main software repository of Ubuntu 13.04.

It's not installed by default.

-

Hm, this will not copy the escape codes, but just to grab the text, you can go to menu Edit -> Select All, then press CtrlShiftC and either (i) paste the clipboard to an editor or (ii) type cat > filename and press CtrlShiftV.

-
This is a different use case - As explicitly stated, I want to include the ANSI escape codes. –  l0b0 Aug 30 '12 at 14:58

man history look for history expansion for escaped characters. I believe history can do what youre asking.

-
I could find nothing relevant in the man page. Please be more specific. –  l0b0 May 1 '13 at 20:28