Many of my notebooks have a similar repeating structure, which is very convenient and reliable for my workflow: a chunk of code defining a Manipulate for exploring some phenomenon, the output of the Manipulate, where the phenomenon can be explored, and then some notes or observations about the phenomenon. When I'm focused on coding, this is fine, but as my focus shifts to the phenomenon itself, the code is distracting and takes up a lot of space, so I'd like to be able to hide or collapse it.

Is there a way to hide or toggle the visibility of code, independently of the results it produces? In effect, what I'm seeking is there reverse of the default behavior, in which code and results that are grouped together can be collapsed to show just the code.

Note that I'm not seeking a way to move the code elsewhere: the point is the be able to easily move back and forth between having the code behind some data or visualization visible, and associated with the output, and having it hidden or collapsed.

  • 1
    $\begingroup$ Come to think of it, I've never used the hide-output/keep-code behavior that's built it; but would always use a hide-code/keep-output feature. $\endgroup$ – orome Nov 12 '12 at 20:53

Double click the output cell instead

Like so

EDIT: From murrays comment: tutorial/WorkingWithCells: "To specify which cells remain visible when the cell group is closed, select those cells and double-click to close the group."

  • 2
    $\begingroup$ big +1, didn't know about this $\endgroup$ – Szabolcs Nov 12 '12 at 20:59
  • 1
    $\begingroup$ Not only did I have no idea about this, but I tried it several times and got nothing to happen! What a great hidden (at least from some of us) trick. $\endgroup$ – orome Nov 12 '12 at 21:01
  • 6
    $\begingroup$ I find the comments interesting, I believe this has been in since v6, and was one of my favorite new features from that version. $\endgroup$ – rcollyer Nov 12 '12 at 21:02
  • 3
    $\begingroup$ @Szabolcs I'm always amused by what is not known. It's hard to separate the arcane from the common in one's own mind I suppose. I wonder how many of the features you take for grated I have no knowledge of? It's plenty I imagine. $\endgroup$ – Mr.Wizard Nov 12 '12 at 21:47
  • 5
    $\begingroup$ The double-click trick is also described in the Documentation Center page tutorial/WorkingWithCells: "To specify which cells remain visible when the cell group is closed, select those cells and double-click to close the group." $\endgroup$ – murray Nov 13 '12 at 16:29

You can create a palette to show/hide all the input cells in the selected notebook.

        Button["Hide code", {
            NotebookFind[SelectedNotebook[], "Output", All, CellStyle];
            FrontEndExecute[FrontEndToken[SelectedNotebook[], "SelectionCloseUnselectedCells"]]
        Button["Show code", NotebookFind[SelectedNotebook[], "Input", All, CellStyle]]
  • 2
    $\begingroup$ Nice! It seems it does not hide cells without output such as 1+1; $\endgroup$ – chris Nov 13 '12 at 7:11
  • $\begingroup$ @chris Is there a way to hide it otherwise? I don't think you can fold a single cell. $\endgroup$ – rm -rf Nov 13 '12 at 15:00
  • 1
    $\begingroup$ You could set Cell Open to False. $\endgroup$ – Mr.Wizard Nov 13 '12 at 17:28
  • 1
    $\begingroup$ How to save the palette for future re-use? $\endgroup$ – qazwsx Oct 24 '14 at 4:34

In version 9: suppose nb is your notebook object (this can be nb=EvaluationNotebook[], but if you're going to be closing input cells you probably want to have an auxilary notebook a choose the correct notebook from Notebooks[]). Then, to close all the Input cells, for example, do

cells = Cells[nb, CellStyle->"Input"]
SetOptions[#, CellOpen -> False] & /@ cells

To open them back up

SetOptions[#, CellOpen -> True] & /@ cells

Obviously, you can select any style cell to close and open


I have to do this all the time. I only have an annoyingly manual way to solve this. On a mac, Alt-click on the cell marker on the right for an InputCell containing the code, this should select ALL input cells. Then go to the Cell -> Cell Properties menu at the top and unselect the Open item. All the input cells should hide. On the right there should still be tiny little input cell markers. To open them follow the same procedure and select the Open again.

The answer by "rm -rf" is great! Thanks.


Alternatively, remove all inputs entirely: (CAUTION)

Copy-pasting the entire notebook to another, new notebook, then pasting the following in it will delete all input cells rather than minimizing them, such that you could send it to someone if you only wanted them to have the output:

 nb = EvaluationNotebook[];
 NotebookFind[EvaluationNotebook[], "Input", All, CellStyle];

Though, be careful with this method as it deletes all input cells (including itself) in the respective notebook.


Following the solution given by Sjoerd de Vreis and Itai Seggev, I found useful define this pair of functions using their procedure as follows.

  • Function for closing all cells inputs:

    CloseAllInputsCells[] := Module[{nb, cells},
      nb = EvaluationNotebook[];
      cells = Cells[nb, CellStyle -> "Input"];
      SetOptions[#, CellOpen -> False] & /@ cells;
  • Function for recovering or made visible again:

     OpenAllInputsCells[] := Module[{nb, cells},
      nb = EvaluationNotebook[];
      cells = Cells[nb, CellStyle -> "Input"];
      SetOptions[#, CellOpen -> True] & /@ cells;

How to use it:

  1. Copy / Paste both functions above in a fresh / new notebook and run the notebook.
  2. Inside your target Notebook, at the end of your document, run CloseAllInputsCells[] . All input cells will be closed.
  3. For recovering all inputs again, run OpenAllInputsCells[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.