98

Does anyone know what is the keyboard shortcut to clear (not toggle) the cell output in Jupyter Notebook?

nbro
  • 15,395
  • 32
  • 113
  • 196
zesla
  • 11,155
  • 16
  • 82
  • 147

10 Answers10

97

You can setup your own shortcut in the UI (for the latest master version):

enter image description here

This menu can be found in Help > Keyboard Shortcuts in any open notebook.

Alejandro
  • 64
  • 7
Dmitrii Magas
  • 1,125
  • 10
  • 8
  • 33
    Specifically, this is under `Help > Edit Keyboard Shortcuts` in any open notebook. – Nate Aug 17 '17 at 00:08
  • 1
    Great suggestions above. I personally like to clean the output from CLI so thought would share that info here as well `jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace Notebook.ipynb` More info --> https://stackoverflow.com/questions/28908319/how-to-clear-an-ipython-notebooks-output-in-all-cells-from-the-linux-terminal – Pramit Mar 06 '19 at 19:00
  • 3
    This option is not there in Jupyter Lab – ColinMac Feb 05 '20 at 17:46
  • 1
    This is in Jupyter notebook. Anything similar in Jupyterlab? – Uri London Dec 29 '22 at 07:52
75

For versions less than 5:

Option 1 -- quick hack:

Change the cell type to raw then back to code: EscRY will discard the output.

Option 2 -- custom shortcut (without GUI):

For this, you need to edit the custom.js file which is typically located at ~/.jupyter/custom/custom.js (if it doesn't exist, create it).

In there, you have to add

require(['base/js/namespace']) {
    // setup 'ctrl-l' as shortcut for clearing current output
    Jupyter.keyboard_manager.command_shortcuts
           .add_shortcut('ctrl-l', 'jupyter-notebook:clear-cell-output');
}

You can add shortcut there for all the fancy things you like, since the 2nd argument can be a function (docs)

If you want mappings for other standard commands, you can dump a list of all available commands by running the following in your notebook:

from IPython.core.display import Javascript

js = """
  var jc_html = "";
  var jc_array = Object.keys(IPython.notebook.keyboard_manager.command_shortcuts.actions._actions);
  for (var i=0;i<jc_array.length;i++) {
    jc_html = jc_html + jc_array[i] + "<br >";
  }
  element.html(jc_html);
  """

Javascript(data=js, lib=None, css=None)
Ciprian Tomoiagă
  • 3,773
  • 4
  • 41
  • 65
20

Add following at start of cell and run it:

from IPython.display import clear_output
clear_output(wait=True)
devil in the detail
  • 2,905
  • 17
  • 15
14

Just adding in for JupyterLab users. Ctrl, (advanced settings) and pasting the below in User References under keyboard shortcuts does the trick for me.

{
"shortcuts": [
        {
            "command": "notebook:hide-cell-outputs",
            "keys": [
                "H"
            ],
            "selector": ".jp-Notebook:focus"
        },
        {
            "command": "notebook:show-cell-outputs",
            "keys": [
                "Shift H"
            ],
            "selector": ".jp-Notebook:focus"
        }
    ]
}
gem7318
  • 181
  • 1
  • 6
  • 2
    You saved me a lot of time :D – imitusov Jan 26 '20 at 12:15
  • 1
    @ИльяМитусов super glad to hear it! I just dropped my current key bindings into [this](https://github.com/GEM7318/JupyterLab-Custom/blob/master/KeyBindings.txt) repo in case they're helpful and will keep it up to date as best I can. – gem7318 Feb 27 '20 at 14:39
  • This is the third time in maybe 3-5 years I've found myself back at this answer to this question after googling "command clear cell output jupyter lab". I really appreciate the awareness users like you bring to these Q/A's that the question asker's needs often don't reflect everyone's who come here. – Wassadamo Apr 29 '22 at 18:05
  • Thank you @Wassadamo :) – gem7318 Aug 03 '23 at 16:00
5

Go to Help -> Edit Keyboard Shortcuts.

enter image description here

Then add the Shortcut you desire to the "Clear Cell" field.

enter image description here

Pavindu
  • 2,684
  • 6
  • 44
  • 77
Vedhasankaran
  • 51
  • 1
  • 3
4

Depends if you consider the command palette a short-cut. I do.

  1. Press 'control-shift-p', that opens the command palette.
  2. Then type 'clear cell output'. That will let you select the command to clear the output.

enter image description here

Donal
  • 8,430
  • 3
  • 16
  • 21
2

To delete/clear individual cell outputs in JupyterLab (without going to Edit > Clear Output), go to Settings > Advanced Settings Editor (Ctrl+,) > Keyboard Shortcuts and add this to "shortcuts":

{
  "command": "notebook:clear-cell-output",
  "keys": [
    "Shift D",
    "Shift D"
  ],
  "selector": ".jp-Notebook:focus"
}

And save it! (Ctrl + S)

Then when you are in the editor, just press Esc to escape the edit mode and press Shift + d + d.

1

You can do this via the command line: jupyter nbconvert --ClearOutputPreprocessor.enabled=True --inplace *.ipynb

Markus
  • 21
  • 2
1

This is not a keyboard shortcut but to clear all output you can click the following buttons:

enter image description here

Erich Purpur
  • 1,337
  • 3
  • 14
  • 34
0

I just looked and found cell|all output|clear which worked with:

Server Information: You are using Jupyter notebook.

The version of the notebook server is: 6.1.5 The server is running on this version of Python: Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)]

Current Kernel Information: Python 3.8.3 (tags/v3.8.3:6f8c832, May 13 2020, 22:37:02) [MSC v.1924 64 bit (AMD64)] Type 'copyright', 'credits' or 'license' for more information IPython 7.19.0 -- An enhanced Interactive Python. Type '?' for help.

Ray Tayek
  • 9,841
  • 8
  • 50
  • 90