In the Python window, right-click and "Save as". This will save all the typed commands...working or not, without all the returned messages. You can then clean up the file, and actually "Load" it to have it execute in the window again.
Also, I have the "Add to Results" checked, so anything that runs here is also added to the results.
But, if it crashes before you get to save, or close out successfully (for the results)...I'm not sure, and would also be curious. Maybe you can have a script that always does an auto "Save as"???
Thanks, Rebecca. I wasn't aware that there was a right-click context menu for the Python window.
My current "safety" method is to select my script and copy/paste to an external text editor in case of crash. I don't necessarily want to save script files for all my scratch scripts, and using "Save As..." I would still have to remember to do it before a crash. "Add to Results" only adds geoprocessing tool runs, and my scripts are generally more cursor-based, so that doesn't quite do what I need.
In any case, I suppose the answer is to do the bulk of my work in a proper IDE that doesn't crash nearly as often as ArcMap, and also enables my compulsive "Ctrl+S" saving habit.
edit: I'm still holding out hope that someone knows of a secret log file that records Python window activity.
I can relate.....really bums me out after long stretch of testing to have it crash and lose everything. I do the same as you many times...mass copy-paste. Nice thing about the SaveAs however, you don't have to clean up all the >> and returned messages....just what you typed. So worth it when you remember. Many times my script rely on the open MXD so can't always strictly use the IDE (I use Wing Pro myself)
But good to know it already exists in ArcGIS Pro....if it does actually keep it as you go, and doesn't rely on a clean exit (i.e. if it saves even with a crash....)
In ArcGIS Pro there is. If you look under the folder C:\Users\<user_name>\AppData\Local\ESRI\Local Caches, there is a file name py_repl.history that includes code run from the Python window.