# How to fold output cells by default?

In Mathematica the default behavior of output cells is to pop up after the kernel has processed the input cells and got a result. I know I could add ; in the end of input cells to disable output cells, which are often too messy in the middle of my calculations and usually I don't really care what they are, from appearing. However sometimes I need to go back and check some stuff, so it is still necessary to keep those intermediate results there. Then my questions is that is it possible to "fold" output cells? By "folding" I mean it would look as if I manually double click the cell, so the output cell will be hidden and an arrow appears next to the input cell. I tried to find some related settings described in this post, but nothing useful there. Thanks!

-
I'm sorry @Nasser, I don't get it either...It seems like you're talking about putting a series of steps in a single function (using Module or something similar), which I already know (and I choose not to do for some reasons). –  Leo Fang Jul 27 '13 at 4:02
By the way, the jumping cursor you're talking about could be achieved (though taking one more step) by pressing ↓ after evaluating a cell and then pressing shift+enter. Is it what you want? –  Leo Fang Jul 27 '13 at 4:04
yeah mathematica is inefficient in this indeed... –  Leo Fang Jul 27 '13 at 4:11
This is nearly a duplicate of (3235) but that doesn't address folding all output automatically without using the special function. –  Mr.Wizard Jul 27 '13 at 6:27
@Mr.Wizard you are always our wizard! It would be great if it could be done automatically without any special function, but I'm almost satisfied with AutoCollapse since it seems unlikely to have something fancier. –  Leo Fang Jul 27 '13 at 21:19

I'm slow, but eventually I get there. ;-) You need CellEpilog to make the behavior automatic on evaluation of any cell. Importing the code from my autoFoldOutput function:

SetOptions[EvaluationNotebook[],
CellEpilog :>
(SelectionMove[EvaluationNotebook[], All, EvaluationCell];
FrontEndTokenExecute["SelectionCloseUnselectedCells"])
]


Simply evaluate that in your Notebook and all output groups will be automatically closed when they are created. You may replace EvaluationNotebook[] with $FrontEndSession if you want to affect all Notebooks during the session or with $FrontEnd to persistently affect all Notebooks.

-
This is awesome Mr. Wizard! One quick question: Will the effect of using $FrontEnd continue even the kernel is closed and then reactivated? – Leo Fang Jul 29 '13 at 1:52 @Leo I'm glad you like it. :-) Yes, settings to $FrontEnd should persist between sessions; this is the same as setting them in the Global Preferences item of the Option Inspector (but don't try setting this complicated definition from there). They can be viewed and cleared from there as well (as text will show everything), so you don't have to worry about not being able to restore default behavior. –  Mr.Wizard Jul 29 '13 at 8:20
Good to know! Thanks. :) –  Leo Fang Jul 29 '13 at 17:41

Not sure if this is what you're looking for, but the Cell menu has an item that collapses open cells:

Here I highlighted the open input/output cells and then collapsed them to get the following:

If your question is about automatically and immediately collapsing every output cell, then that's a different story. It will most likely involve this answer.

-
Thanks Jens! I think Mr. Wizard's AutoCollapse could provide the effect I expect! It seems that when an output cell is generated, the selection is moved to the input cell by AutoCollapse, leaving the output cell unselected and then closing it by SelectionCloseUnselectedCells, which is effectively the same as the Close Unselected Cells button in your screenshot. I just wonder how do I rewrite AutoCollapse to make it do the opposite way, because I didn't see any token providing the effect of SelectionClose"Selected"Cells. –  Leo Fang Jul 27 '13 at 6:04
Aha! Mr. Wizard has provided another post which has the desired function. –  Leo Fang Jul 27 '13 at 21:10