38

I've been using Jupyter Notebooks for a couple of years now. I've just headed over to Jupyter Lab, but I've found the lack of shortcuts to be a burden.

For example, I noticed that I can search for commands in the left hand palette. But I can't seem to easily bind them to a keyboard shortcut. Is this even possible?

For example, I want to collapse the current cell output with "O" and collapse all code cells with "Shift O".

enter image description here

Daniel Severo
  • 1,768
  • 2
  • 15
  • 22
  • Are you able to tune the JupyterLab to a more usable setting yet or are you still stick with NB – xappppp Sep 18 '18 at 21:07

6 Answers6

38

This question is answered on GitHub here. You can also look here for the correct command names to enter in your keyboard shortcut user overrides because they are not always the same as what is shown in the Commands side-bar.

The following are some that I use:

{
  "shortcuts": [
    {
      "command": "notebook:hide-cell-outputs",
      "keys": [
        "O"
      ],
      "selector": ".jp-Notebook:focus"
    },    
    {
      "command": "notebook:show-cell-outputs",
      "keys": [
        "O", 
        "O"
      ],
      "selector": ".jp-Notebook:focus"
    },    

    {
      "command": "notebook:hide-all-cell-outputs",
      "keys": [
        "Ctrl L"
      ],
      "selector": ".jp-Notebook:focus"
    },

    {
      "command": "notebook:hide-all-cell-code",
      "keys": [
        "Shift O"
      ],
      "selector": ".jp-Notebook:focus"
    }
  ]
}

which allows you to hide a cell output by pressing O once and showing the cell output by pressing O twice. The last one collapses all cell code with Shift + O as you requested.

Cerberus
  • 8,879
  • 1
  • 25
  • 40
jeschwar
  • 1,286
  • 7
  • 10
  • Thank you for this. I tried this and it works, but it is somehow really slow. It takes over 1 second to collapse the cell output after pressing 'O'. Do you have any idea what may cause this? If I go to View -> Collapse Selected Output, it is instant. (I'm on macOS, if that matters at all) – Fabian Ying Jun 07 '18 at 14:23
  • 3
    Ah, I have found the reason why: The second shortcut uses O as well and therefore it is waiting to see whether a second O is typed. If I comment out the second keyboard shortcut, it works as expected. – Fabian Ying Jun 07 '18 at 14:41
  • Nice, things here, is there a code can do the hide-selected-cell-code? – xappppp Sep 18 '18 at 17:20
  • 1
    @jeschwar: Is there a clean list of all the possible commands that one could include in the keyboard shortcut overrides? It seems strange that they don't have a master list there where you make the changes. Otherwise, how does one know the names of the commands? – teepee Nov 20 '18 at 02:23
  • @teepee: I look at the [source code](https://github.com/jupyterlab/jupyterlab/blob/master/packages/notebook-extension/src/index.ts#L70) to figure out which command names I need. A master list would be useful but I find it's straight-forward to find what I need in that link. – jeschwar Nov 20 '18 at 16:37
  • Hi, @jeschwar, I copied your code and pasted in the User Preferences of the Settings in Jupyter lab but got this error `[additional property error] notebook: collpase-selected-outputs`. Do you happen to know why? Thanks – Nemo Sep 05 '19 at 01:53
  • 1
    @Nemo From [here](https://github.com/jupyterlab/jupyterlab/issues/6865#issuecomment-565679065) "You need to format it so that there's one "shortcuts" list with several entries:". – Wes Turner Dec 27 '19 at 13:56
  • Where can we find a list of command and selector to create our own shortcut? – BND Jun 19 '20 at 10:12
  • I still come by and copy the setting. – pakira79 Apr 29 '21 at 02:30
20

On keyboards shortcuts of advance settings this code works fine for moving cells up and down

{
    // Move cell up
    "shortcuts": [
        {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-up",
      "keys": [
        "Alt ArrowUp"
      ]
        },
    // Move cell down
        {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-down",
      "keys": [
        "Alt ArrowDown"
      ]
    }
    ]

}

move cell up and down

MertTheGreat
  • 500
  • 1
  • 7
  • 20
9

I use these settings to bind the actions to move a cell up/down to Ctrl + Up/Down:

{
    // Move cell up
    "notebook:move-cell-up": {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-up",
      "keys": [
        "Ctrl ArrowUp"
      ]
    },

    // Move cell down
    "notebook:move-cell-down": {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-down",
      "keys": [
        "Ctrl ArrowDown"
      ]
    }
}
pX0r
  • 1,472
  • 12
  • 14
3

pX0r and plalanne's answers above combined worked for me with minor modification for Mac.

I hope this step-by-step iteration is helpful for someone like me who's a baby programmer. To summarize:

  1. Open Advanced Settings Editor under the Settings tab, or command , in Mac.
  2. Navigate to Keyboard Shortcuts. You should see the screen plalanne answered with.
  3. Use pX0r's codes, however making one change in the key binding as Ctrl Arrowup is reserved in Mac to view all running applications (if you have it set up that way). Similarly, Shift Arrowup is for selecting multiple cells. As a result, I opted for Alt Arrowup. Notice the key on your Mac keyboard says alt/option. You have to refer to it as Alt to work. There you have it. Copy the codes below to User Overrides which is the right pane.
  4. Re-open your notebook and test if it works as intended.
  5. You can customize more keys in this fashion as long as it is defined here on GitHub. For the most part, all that you need are the command IDs starting line 72.
{
    // Move cell up
    "notebook:move-cell-up": {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-up",
      "keys": [
        "Alt ArrowUp"
      ]
    },

    // Move cell down
    "notebook:move-cell-down": {
      "selector": ".jp-Notebook:focus",
      "command": "notebook:move-cell-down",
      "keys": [
        "Alt ArrowDown"
      ]
    }
}
oustella
  • 63
  • 5
1

You should edit the settings file in Settings/Keyboard Shortcuts. Here :

Jupyter lab keyboard shortcut

There you can specify any custom shortcut that you would like!

plalanne
  • 1,010
  • 2
  • 13
  • 30
1

If you cannot save the "User Preferences" settings and get a syntax error

[additional property error] command is not a valid property

you have probably missed to nest within the "shortcuts" list, as described here. Additionally, to override an old setting you do the following, using Activate Next Tab and Activate Previous Tab as examples:

{
    "shortcuts": [
        {
            "command": "application:activate-next-tab",
            "keys": [
                "Ctrl Shift ]"
            ],
            "selector": "body",
            "disabled": true  // disable old setting
        },
        {
            "command": "application:activate-previous-tab",
            "keys": [
                "Ctrl Shift ["
            ],
            "selector": "body",
            "disabled": true  // disable old setting
        },
        {
            "command": "application:activate-next-tab",
            "keys": [
                "Ctrl 1"  // enable new shortcut key
            ],
            "selector": "body"
        },
        {
            "command": "application:activate-previous-tab",
            "keys": [
                "Ctrl 2"  // enable new shortcut key
            ],
            "selector": "body"
        }
    ]
}

Now you can click save and refresh your browser for the new setttings to take effect.

Samuel
  • 2,895
  • 4
  • 30
  • 45