Search code examples
jupyter-notebookkeyboard-shortcutsjupyter-lab

Is there a way to use Jupyter Notebooks without modes?


I am using jupyter-lab, and was wondering if there is a plugin that allows me to work without switching between command and edit modes?

I'm constantly cutting, pasting, and undoing cells because I accidentally switch into Command mode when I mean to be typing the characters 'x', 'y', or 'z' in Edit mode.

Some context: the concept of modes is integral to Jupyter notebooks. You are either in Command mode (Esc) or Edit mode (Enter), but I would like to use Jupyter Notebooks with a single set of shortcuts, as if I was editing a single document. #nomodes.


Solution

  • Aside from the default shortcuts, there is actually very little that's mode-specific in JupyterLab. Most commands that are run in command mode can be run without first exiting edit mode.

    We can get pretty close to modeless editing in Jupyter by using a shortcut that's independent of the current mode. There are instructions on how to customize the keyboard shortcuts here.

    As an example, the default shortcut for the cut-cell command is x. This could be replaced with the shortcut Accel Ctrl X. That can be used in either mode without interfering with editing. Just add the following into the User Preferences panel:

    {"shortcuts": [
      {
        "command": "notebook:cut-cell",
        "disabled": true,
        "keys": ["X"],
        "selector": ".jp-Notebook:focus"
      },
      {
        "command": "notebook:cut-cell",
        "keys": ["Accel Ctrl X"],
        "selector": "body"},
      }
    ]}
    

    Each default command mode shortcut should be modified as follows:

    1. Prefix a universal modifier to the shortcut (e.g. Accel Ctrl). Accel is just the Jupyter term for the Super/Command/Windows key on your keyboard.
    2. Maintain the same shortcut keys for edit mode. The key insight is to use a "selector": "body". That selector can target both modes.
    3. As with any default shortcut override, make sure to mark the original as "disabled: true".

    Alternatively you could duplicate the same shortcut twice:

    • one for edit mode with "selector": ".jp-Notebook.jp-mod-editMode".
    • one for command mode with "selector": ".jp-Notebook:focus".

    The default shortcuts for notebook:run-cell are set up this way actually. But I have not run into trouble with using "body" as the selector.

    Just a note: this is a fairly tedious and error-prone exercise. There are dozens of default shortcuts that need to be modified.