The following definition to $PreRead$Pre
will save a copy of your notebook as filename.nb.backup.nb
, where filename.nb
is your original notebook and strip it of all output cells every time you evaluate a cell.
$PreRead$Pre = (With[
{nb = NotebookPut[NotebookGet[EvaluationNotebook[]]]},
FrontEndExecute[{
FrontEnd`NotebookFind[nb, "Output", All, CellStyle, AutoScroll -> False],
FrontEnd`FrontEndToken["Clear"]
}];
NotebookSave[nb, NotebookFileName[] <> ".backup"];
NotebookClose[nb] NotebookClose[nb];
]; #
] &)