BeeePollen
BeeePollen

Reputation: 553

VS Code editing .ipynb file: How to collapse notebook cells?

I'm editing an .ipynb file in VS Code using the Jupyter Notebook extension. I can't seem to figure out how to collapse (or expand) cells.

Can someone explain to me how to do this (using hotkeys or not using hotkeys), or give ideas for what I might be doing wrong?

I tried each of the hotkeys shown below, under three sets of conditions: 1. with the blue vertical bar on the left-hand side of my cell input selected, 2. with the blue vertical bar on the left-hand side of my cell output selected, 3. with my cursor in my cell input.

"Notebook: Collapse Cell Input" and "Notebook: Collapse Cell Output", with default bindings

Upvotes: 25

Views: 34347

Answers (7)

Afshin Davoudy
Afshin Davoudy

Reputation: 181

Here you can customize keys to Collapse/Expand the Input(s)/Output(s) of All/Active cel(s).

Just add the entire code or the part you need to the keybindings.json file:

{ // To collapse active cell input
    "key": "ctrl+[",
    "command": "notebook.cell.collapseCellInput",
    "when": "notebookCellListFocused && !inputFocus && !notebookCellInputIsCollapsed"
},
{ // To expand active cell input
    "key": "ctrl+[",
    "command": "notebook.cell.expandCellInput",
    "when": "notebookCellInputIsCollapsed && notebookCellListFocused"
},
{ //To collapse active cell output
    "key": "ctrl+]",
    "command": "notebook.cell.collapseCellOutput",
    "when": "notebookCellHasOutputs && notebookCellListFocused && !inputFocus && !notebookCellOutputIsCollapsed"
},
{ // To expand active cell output
    "key": "ctrl+]",
    "command": "notebook.cell.expandCellOutput",
    "when": "notebookCellListFocused && notebookCellOutputIsCollapsed"
},
{ // To collapse all cell inputs
    "key": "ctrl+shift+[",
    "command": "notebook.cell.collapseAllCellInputs"
},
{ // To expand all cell inputs
    "key": "ctrl+shift+alt+[",
    "command": "notebook.cell.expandAllCellInputs"
},
{ // To collapse all cell outputs
    "key": "ctrl+shift+]",
    "command": "notebook.cell.collapseAllCellOutputs"
},
{ // To expand all cell outputs
    "key": "ctrl+shift+alt+]",
    "command": "notebook.cell.expandAllCellOutputs"
}

Change key combinations as you wish.

Upvotes: 1

T3nt4c135
T3nt4c135

Reputation: 112

To take a step further, while clicking on a header or markdown cell, one can use the VSCode shortcut to hide or open multiple cells.

Ctrl+Shift+[ <- Open with Windows

Ctrl+Shift+] <- Close with Windows

++[ <- Open with Mac

++] <- Close with Mac

Selecting a header will close/open all mark down cells until it finds the next header, and markdown will close/open all cells until it reaches the next markdown cell.

Upvotes: 1

Raiccio
Raiccio

Reputation: 273

The exact thing that worked for me is:

  1. Go to File -> Preferences -> Keyboard Shortcuts

  2. Search for 'collapse all cell inputs'

  3. Double click of click the plus sign in front of this option and choose a unique shortcut combination (e.g. Ctrl + Shift + C Ctrl + Shift + A)

You are ready to go, now you can finally collapse all your input cells at once.

Upvotes: 6

Mark
Mark

Reputation: 182671

And another way to collapse cells is coming to v1.64. See

Cell collapsing UI

Notebook cells have a blue bar on the left side to indicate that they are focused. This bar is now interactive - you can click the top part to collapse the cell input, and the bottom part to collapse the output.

cell collapse demo

Upvotes: 66

Vikas Sharma
Vikas Sharma

Reputation: 61

In my VSCode insiders build version 1.61.0, its working.

What's working ->

 a) One shot expand/collapse all cells(code/output) in '.ipynb'(notebook)
 b) Single cell code/output expand/collapse in '.ipynb'(notebook)
 c) code folding in `.py` file with `#%%` (hierarchy style)
  1. for .ipynb (your requirement)

    There is keybinding available: jupyter.notebookeditor.expandallcells/collapseallcells

    what it does?

            Expands/collapses all cells in one shot
    

    Find this setting->

         Top menu File->Preferences->Keyboard Shortcuts
         In search("Type to search in keybindings"...top side) 
         type "Notebook Expand" or "Notebook Collapse", check out:
    
                 Notebook: Expand/Collapse All Cells, 
                 Notebook: Expand/Collapse Cell Input and 
                 Notebook: Expand/Collapse Cell Output
    
    
         Its possible to expand/collapse single/all cells with these shortcuts keys.
    

To make this answer complete (others have answered)->

    1.  double click gutter area (immediate space on left side of code cell) to Expand/Collapse cell (code/output)

    2.  right click on gutter area to get Expand/Collapse cell (code/output)
  1. Detailed post for '.ipynb' and '.py' here

Upvotes: 6

codeananda
codeananda

Reputation: 1318

I'm using version 1.58.2 and this functionality is now available!

Simply right-click a code cell and select 'Collapse Cell Output'.

Alternatively, click a code cell (make sure you are in command mode) and use the keyboard shortcut ⌘K T (on Mac) or Ctrl-K T on Windows.

Collapse Cell Output

Upvotes: 5

Jill Cheng
Jill Cheng

Reputation: 10372

Currently, the use of this function is not supported in Jupyter of VS Code, you could refer to this link: Jupyter notebook cell code folding and output view expanding request.

Also, please try to use "Visual Studio Code Insiders", right-click in the cell of Jupyter, and select "Collapse Cell Input" or "Collapse Cell Output":

before:

enter image description here

after:

enter image description here

Upvotes: 4

Related Questions