Search code examples
pythonvisual-studio-codejupyter-notebookjupyter

In a Jupyter Notebook open in VS Code, how can I easily run a cell and advance to the next cell?


In VS Code, while working with Jupyter Notebooks, I am trying to run a cell and then advance to the next cell and keep the cursor in that cell. But after I advanced to the next cell, I have to click that cell to start writing my code. Is there any way to do that automatically?


Solution

  • If you're talking about Notebook editor, then I assume you're referring to the shift+enter keyboard shortcut that is bound by default to the command notebook.cell.executeAndSelectBelow. And yes, it selects the next cell as a whole and does not move focus into the input editor section of the cell. You should be able to move focus into the cell by pressing enter.

    If that's not good enough for you, you can put this in your keybindings.json file (open this file using Preferences: Open Keyboard Shortcuts (JSON) in your command palette):

    // option 1 (just pick one option. I'm pretty sure they're the same)
    {
        "key": "ctrl+b",
        "command": "runCommands",
        "args": {
            "commands": [
                "notebook.cell.executeAndSelectBelow",
                "notebook.cell.edit",
            ],
        },
    },
    // option 2
    {
        "key": "ctrl+b",
        "command": "runCommands",
        "args": {
            "commands": [
                "notebook.cell.execute",
                "notebook.focusNextEditor",
            ],
        },
    },
    

    If you're talking about interactive windows and Python files with code cell magics, then either use shift+enter, which is bound to jupyter.runcurrentcelladvance by default, or use the jupyter.interactiveWindow.textEditor.autoMoveToNextCell setting if you want it to happen whenever you run a cell, regardless of whether you use shift+enter.