Apply Now

Prefer using email? Say hi at hello@datasciencesouth.com

Customizing Jupyter Lab Shortcuts

How to setup custom keyboard shortcuts for Jupyter Lab.

It took us far too long to start using custom shortcuts in Jupyter Lab.

The default shortcuts in Jupyter Lab are good (take a look at our Jupyter Lab guide for a list of the most important default shortcuts), but there are a few parts of the data science workflow that the default shortcuts don’t cover.

Adding just a few custom shortcuts will lead to a major increase in the speed of your workflow.

This article will:

  • show you how to customize shortcuts in Jupyter Lab
  • give you a starter template on useful custom shortcuts

How to customize shortcuts in Jupyter Lab

You can access the Keyboard Shortcuts menu using:

  • Menu Bar -> Settings
  • Settings -> Advanced Settings Editor
  • Advanced Settings Editor -> Keyboard Shortcuts
  • Keyboard Shortcuts -> User Preferences

If you haven’t edited your Jupyter Lab shortcuts before, you’ll have nothing in the User Preferences section.

There are a few strategies for setting up User Preferences:

  • copy all the System Defaults into User Preferences, and then modify the shortcuts you want to change
  • only put the shortcuts we want to change into User Preferences, which is what we will do in this article

Either way, you’ll benefit from having a copy of System Defaults open in a text editor, for easier searching (the Jupyter Lab search can leave a little wanting :).

What do we want from custom shortcuts?

We have custom shortcuts setup for:

  • restarting the kernel
  • running all cells above this cell
  • opening a terminal
  • moving between tabs

We will go through all these in steps - you can see complete JSON for all these at the end of the post.

Restarting the kernel & running all cells above

The most important custom shortcuts are those that make restarting the kernel quicker. Its something you do a lot when using notebooks.

Which shortcut is best for you depends on how you like to restart the kernel - we use Restart Kernel & Run All the most. The full shortcuts we use for managing the kernel are:

  • restart & run all -> F6
  • restart & clear -> F7
  • run all above -> F8

Copying the JSON below directly into your User Preferences will give you these three custom shortcuts:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "shortcuts": [
        {
            "command": "runmenu:restart-and-run-all",
            "keys": [
                "F6"
            ],
            "selector": "[data-jp-code-runner]"
        },
        {
            "command": "kernelmenu:restart-and-clear",
            "keys": [
                "F7"
            ],
            "selector": "[data-jp-kernel-user]:focus"
        },
        {
            "command": "kernelmenu:run-all-above",
            "keys": [
                "F8"
            ],
            "selector": "[data-jp-kernel-user]:focus"
        }
    ]
}

You’ll notice that we don’t use F5 - because this is often used as a Refresh Page shortcut in browsers.

Open a terminal

We have this mapped to Alt T. A common workflow for with this shortcut is:

  • Alt T to open a terminal
  • do some work
  • Alt ArrowUp to switch back and forth with previous tab (usually a notebook)
  • Ctrl W to close the terminal window

Alt ArrowUp is a custom shortcut as well - see the Move between tabs section below.

1
2
3
4
5
6
7
8
9
10
11
{
    "shortcuts": [
        {
            "command": "terminal:open",
            "keys": [
                "Alt T"
            ],
            "selector": "body"
        }
    ]
}

Move between tabs

The final shortcuts are to make moving around between open tabs using Alt + arrow keys:

  • Alt ArrowUp -> previously used
  • Alt ArrowLeft -> previous tab
  • Alt ArrowRight -> next tab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
{
    "shortcuts": [
        {
            "command": "tabsmenu:activate-previously-used-tab",
            "keys": [
                "Alt ArrowUp"
            ],
            "selector": "body"
        },
        {
            "command": "application:activate-previous-tab",
            "keys": [
                "Alt ArrowLeft"
            ],
            "selector": "body"
        },
        {
            "command": "application:activate-next-tab",
            "keys": [
                "Alt ArrowRight"
            ],
            "selector": "body"
        }
    ]
}

The full custom shortcuts JSON

Below is a copy of the full JSON we use to customize our shortcuts - you can copy this directly into User Preferences:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
{
    "shortcuts": [
        {
            "command": "sidebar:toggle",
            "keys": [
                "F9"
            ],
            "selector": ".jp-SideBar"
        },
        {
            "command": "runmenu:restart-and-run-all",
            "keys": [
                "F6"
            ],
            "selector": "[data-jp-code-runner]"
        },
        {
            "command": "kernelmenu:restart-and-clear",
            "keys": [
                "F7"
            ],
            "selector": "[data-jp-kernel-user]:focus"
        },
        {
            "command": "kernelmenu:run-all-above",
            "keys": [
                "F8"
            ],
            "selector": "[data-jp-kernel-user]:focus"
        },
        {
            "command": "terminal:open",
            "keys": [
                "Alt T"
            ],
            "selector": "body"
        },
        {
            "command": "tabsmenu:activate-previously-used-tab",
            "keys": [
                "Alt ArrowUp"
            ],
            "selector": "body"
        },
        {
            "command": "application:activate-previous-tab",
            "keys": [
                "Alt ArrowLeft"
            ],
            "selector": "body"
        },
        {
            "command": "application:activate-next-tab",
            "keys": [
                "Alt ArrowRight"
            ],
            "selector": "body"
        },
        {
            "command": "notebook:change-cell-to-code",
            "keys": [
                "W"
            ],
            "selector": ".jp-Notebook:focus"
        },
        {
            "command": "notebook:change-cell-to-markdown",
            "keys": [
                "M"
            ],
            "selector": ".jp-Notebook:focus"
        }
    ]
}

Thanks for reading!

If you enjoyed this post, make sure to take a look at our Jupyter Lab guide, for more tips & tricks for getting the most out of Jupyter Lab.