177

Is there a shortcut to run all cells in an IPython notebook?

And if not, does this have a specific reason?

user3017048
  • 2,711
  • 3
  • 22
  • 32
  • Please let me know if you found a way to do this - it would be extremely useful!!! – makansij Sep 24 '16 at 16:51
  • 11
    The closest thing I could find was to enter the command mode by `command` + `shift` + `P` and then type `run all cells` and press enter? – makansij Sep 24 '16 at 16:56
  • No. Press `esc` + `H` to show keyboard shortcuts. Shortcuts are sparing due to limited options that do not interfere with existing browser shortcuts. – pylang Nov 22 '16 at 03:38
  • 2
    `Ctrl` + `shift` + `p` opens up a private browsing window in firefox for me! – Farid Alijani Oct 11 '19 at 15:36
  • 3
    Working perfectly with Anaconda. I clicked Help > Edit Keyboard Shortcuts > Run All Cells and added Shift-R (my choice). – Davidson Lima May 03 '20 at 22:37
  • @DavidsonLima, can this be done programmatically from another notebook? (same kernel) – alancalvitti Apr 20 '21 at 14:28

10 Answers10

119

For the latest jupyter notebook, (version 5) you can go to the 'help' tab in the top of the notebook and then select the option 'edit keyboard shortcuts' and add in your own customized shortcut for the 'run all' function.

Arindam
  • 2,116
  • 1
  • 13
  • 10
  • 9
    This is the answer! Thanks a lot! Maybe one small comment for those who are a bit "blind" like me: You have to click on "add shortcut" (seems greyed out) and then literally type the shortcut you want to use. – user3017048 Feb 14 '18 at 07:33
  • 1
    As of Jupyter 5.5 you can go to Kernel -> Restart and Run All. This will run all cells from top to bottom. – Chinmoy Nov 26 '18 at 13:53
  • 7
    How do you actually type the shortcut? Nothing is working for me when I type. – mathtick Mar 04 '19 at 08:29
  • 1
    @mathtick, this is from a while ago but you in the editing window you'll see a "details of defining keyboard shortcuts" link which details how to type out new shortcuts. Essentially you have to type of 'Cmd' for command, instead of pressing 'Cmd'. – exlo Jul 08 '19 at 02:34
  • @exlo , I'm on opera browser, don't see any "details of defining keyboard shortcuts".... I get the same as mathtick – adiro Sep 19 '19 at 12:09
  • @adiro From your notebook, go to 'Help' > 'Keyboard Shortcuts' and select 'Edit Shortcuts'. In that window 'Edit Command mode Shortcuts', you'll see a blue link in the notice at the top. Clicking that link gives you details on how to add shortcuts. In my earlier message I said that you have to type out 'Cmd' for the shortcut instead of hitting the cmd button on your keyboard. – exlo Sep 23 '19 at 02:24
  • 2
    @exlo No blue link in opera browser.... just a box with a title "edit command mode shortcuts", an OK button and nothing else..... – adiro Sep 24 '19 at 06:30
94

Easiest solution:

Win/Linux:

  1. Esc (unselect potential cells or edit modes)

  2. CtrlA (select all cells)

  3. ShiftEnter (run all selected)

Mac:

  1. Esc (unselect potential cells or edit modes)

  2. A (select all cells)

  3. ShiftEnter (run all selected)

blkpingu
  • 1,556
  • 1
  • 18
  • 41
Vitor Abella
  • 1,213
  • 8
  • 15
61

There is a menu shortcut to run all cells under Cell > "Run All". This isn't bound to a keyboard shortcut by default- you'll have to define your own custom binding from within the notebook, as described here.

For example, to add a keyboard binding that lets you run all the cells in a notebook, you can insert this in a cell:

%%javascript

Jupyter.keyboard_manager.command_shortcuts.add_shortcut('r', {
    help : 'run all cells',
    help_index : 'zz',
    handler : function (event) {
        IPython.notebook.execute_all_cells();
        return false;
    }}
);

If you run this code from within iPython notebook, you should find that you now have a keyboard binding to run all cells (in this case, press ctrl-M followed by r)

emunsing
  • 9,536
  • 3
  • 23
  • 29
18

Jupyter Lab 1.0.4:

  1. In the top menu, go to: Settings->Advanced Settings Editor->Keyboard Shortcuts

  2. Paste this code in the User Preferences window:

{
    "shortcuts": [
        {
            "command": "runmenu:run-all",
            "keys": [
                "R",
                "R"
            ],
            "selector": "[data-jp-kernel-user]:focus"
        }
    ]
}
  1. Save (top right of the user-preferences window)

This will be effective immediately. Here, two consecutive 'R' presses runs all cells (just like two '0' for kernel restart). Notably, system defaults has empty templates for all menu commands, including this code (search for run-all). The selector was copied from kernelmenu:restart, to allow printing r within cells. This system defaults copy-paste can be generalized to any command.

Ran Feldesh
  • 1,111
  • 11
  • 17
8

I've been trying to do this in Jupyter Lab so thought it might be useful to post the answer here. You can find the shortcuts in settings and also add your own, where a full list of the possible shortcuts can be found here.

For example, I added my own shortcut to run all cells. In Jupyter Lab, under Settings > Advanced Settings, select Keyboard Shortcuts, then add the following code to 'User Overrides':

{
    "notebook:run-all-cells": {
      "command": "notebook:run-all-cells",
      "keys": [
        "Shift Backspace"
      ],
      "selector": ".jp-Notebook.jp-mod-editMode"
    }
}

Here, Shift + Backspace will run all cells in the notebook.

johntellsall
  • 14,394
  • 4
  • 46
  • 40
lstodd
  • 168
  • 2
  • 9
  • 1
    Just to note, that equivalently, `Shift-BackSpace` can be entered in the "Edit Keyboard Shortcuts" UI dialog, for the command of your choice, to yield the same result. – matanster Jul 14 '18 at 09:40
6

In windows/linux:

Ctrl + A (Select all)

Ctrl + Enter (Run selected cells)

PJ_
  • 473
  • 5
  • 9
4

As of 5.5 you can run Kernel > Restart and Run All

lisp-ceo
  • 119
  • 1
  • 4
1

If you're working in JupyterLab, you can edit the JSON settings directly to add a simple keyboard shortcut to "run all cells".

  1. In the Jupyter window, go to "Settings" --> "Advanced Settings Editor".
  2. Click on the "JSON Settings Editor" button in the top right.
  3. In the "User Preferences" window, add the following to the top of the JSON file to bind a key like "F9" to "run all cells":
{
    "args": {},
    "command": "runmenu:run-all",
    "keys": [
        "F9"
    ],
    "selector": "[data-jp-code-runner]"
}

If you don't want to keep incrementing your run order numbers and/or want to reset your run order numbers, you can also assign a keybind to "Restart kernel and run all cells", like to "F8":

{
    "args": {},
    "command": "runmenu:restart-and-run-all",
    "keys": [
        "F8"
    ],
    "selector": "[data-jp-code-runner]"
},

Also, here's how to run all cells above or below your current cell:

{
    "args": {},
    "command": "notebook:run-all-above",
    "keys": [
        "Ctrl Shift A"
    ],
    "selector": "[data-jp-code-runner]"
},
{
    "args": {},
    "command": "notebook:run-all-below",
    "keys": [
        "Ctrl Shift B"
    ],
    "selector": "[data-jp-code-runner]"
},

FYI, the "selectors" are CSS selectors, you can inspect the Jupyter window in your browser's dev console to find and see them. The selector "[data-jp-code-runner]" corresponds to the individual Jupyter window tabs.

Also, in JupyterLab, doing "Esc" and "Command + a" and "Shift enter" works but is a bit wrong because it creates a new blank code cell at the bottom of your notebook everytime you do so. What you should do instead is "Ctrl enter" or "Command enter", instead of "Shift enter".

Aeronautix
  • 301
  • 2
  • 13
0

A very simple way to do so with IPython that worked for me in Visual Studio Code is to add the following:

{
    "key": "ctrl+space",
    "command": "jupyter.runallcells"
}

to the keybindings.json that you can access by typing F1 and 'open keyboard shortcuts'.

NerdOnTour
  • 634
  • 4
  • 15
DvdG
  • 724
  • 1
  • 8
  • 15
0

For Windows 10

For latest Jupyter Notebook version go to Help > Edit Keyboard Shortcuts > run all cells > Then click + sign to add shortcut I would type Z then click + again to add > then click OK to save.

Trinh Hieu
  • 379
  • 3
  • 6