# diff-able text-only notebooks?

I would like to save my Mathematica notebooks as plain-text documents, so that I can keep them under git version control.

I thought that File > Save as... + Plain Text would do what I want, but I'm having problems with it.

First, I can't figure out how to "re-import" into Mathematica (in a form that can be readily evaluated) a notebook saved this way.

Second, the human-readable text content is overwhelmed by serializations of graphic objects, etc. Therefore, the output produced by git diff on two versions of such text files is a nightmare.

Third, the human-readable text portions are not all that readable. Of course, some rich formatting cannot be easily converted to text, but I would like to preserve at least some of the indentation found in the original, if possible.

Is there a better approach?

-
have you tried saving as an m-file? (ie save as->mathematica package?) You might also want to kill all output before doing that – acl Apr 15 '13 at 18:44
In case it is useful, Mathematica->format->Option Inspector->Notebook Options->File Options->AutoGeneratedPackage will save a .m file containing the notebook's initialization cells, automatically every time you save the notebook. – Tobias Hagge Apr 15 '13 at 18:52
@TobiasHagge good point. perhaps it's worth mentioning that that, too, will only include the code/initialization cells in executable form. – acl Apr 15 '13 at 19:59
What do you mean when you say "plain text"? I ask because the Mathematica notebook files only contain Wolfram Language expressions encoded in ASCII. – Christopher Cole Jul 3 '14 at 17:03

I deal with this by a) removing all output, b) saving as a package file (m-file).

To remove all output, I have some code from somewhere and put it into a button, inside a palette:

Button["kill out",
FrontEndExecute[{FrontEndNotebookFind[FrontEndSelectedNotebook[],
"Output", All, CellStyle, AutoScroll -> False],
FrontEndFrontEndToken["Clear"]}]
]
`

Clicking this removes all output cells from the selected notebook. Possibly I lifted this code from somewhere (in which case if anybody knows, let me know; maybe it's from here, thanks to SeanD). As Sjoerd points out in a comment, there's a menu function for this, but it requests confirmation first (which this approach doesn't).

I suggest removing the output because otherwise you end up with the serialized representations of graphics etc, as you have noted.

Also note that this approach saves code cells (or more generally, as rm-rf points out, initialization cells); everything else gets commented out.

-
There is an "delete all output" option under the Cell menu. – Sjoerd C. de Vries Apr 15 '13 at 19:17
Also, you should probably mention that if you're saving as a package, one should use code cells (or more generally, initialization cells), as otherwise, it will be commented out – R. M. Apr 15 '13 at 19:20
@SjoerdC.deVries yes I should have mentioned that the reason I did this is that the menu function asks for confirmation; the one I gave doesn't. – acl Apr 15 '13 at 19:26
@rm-rf does it save all initialization cells? didn't know that – acl Apr 15 '13 at 19:28
Maybe you lifted the code from mathematica.stackexchange.com/a/8762 – Sean D Jul 3 '14 at 16:48