Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to move Jupyter notebook cells up/down using keyboard shortcut?

Anyone knows keyboard shortcut to move cells up or down in Jupyter notebook? Cannot find the shortcut, any clues?

like image 418
Burhan Avatar asked Jun 18 '20 15:06

Burhan


People also ask

How do you arrange cells in Jupyter Notebook?

Rearrange Cells in a Jupyter Notebook You can change the order of cells within Jupyter Notebook using the up arrow and down arrow buttons on the menu bar. To do this, click inside the cell that you want to move and then press the desired arrow as many times as you need to move the Cell to the desired location.

How do you move multiple cells in Jupyter Notebook?

There are commonly known keyboard shortcuts such as Ctrl + Shift + - to split the cell, Shift + M to merge multiple cells, and Shift + Down / Up to select the cells below or above the selected one.


Video Answer


4 Answers

The following solution works on JupyterLab (I currently have version 2.2.6):

You must first open the Keyboard Shortcuts configuration file. In JupyterLab you can find it in Settings -> Advanced Settings Editor then selecting the "Keyboard Shortcuts" option in the left panel and then editing the "User Preferences" tab at the right.

Expanding on sherdim's answer, you must add two json objects (one for each direction) within the "shortcuts" json array. Here I have chosen the shortcuts Ctrl + Shift + ↓ and Ctrl + Shift + ↑.

{
    "shortcuts": [
        {
            <<other items you may have>>
        },
        {
            "command": "notebook:move-cell-up",
            "keys": [
                "Ctrl Shift ArrowUp"
            ],
            "selector": ".jp-Notebook:focus"
        },
        {
            "command": "notebook:move-cell-down",
            "keys": [
                "Ctrl Shift ArrowDown"
            ],
            "selector": ".jp-Notebook:focus"
        },
    ]
}

Finally, press Ctrl + S to save changes.

Now, when you are in the command mode, you should be able to move one or more selected cells up or down. The shortcuts will even appear in the menu Edit -> Move Cells Up and Edit -> Move Cells Down.

like image 182
David Avatar answered Oct 13 '22 01:10

David


David's answer above was helpful, but didn't work for me in Firefox on Xubuntu. I had to make the following change for the selector:

{
    "shortcuts": [            

{
            "command": "notebook:move-cell-up",
            "keys": [
                "Ctrl Alt Shift ArrowUp"
            ],
            "selector": "body"
        },
        {
            "command": "notebook:move-cell-down",
            "keys": [
                "Ctrl Alt Shift ArrowDown"
            ],
            "selector": "body"
        }
      ]
}
like image 34
Eric Lief Avatar answered Oct 13 '22 00:10

Eric Lief


Further to honeybadger's response, you can see when you open up the Edit Command Mode shortcuts dialog box that there are no shortcuts defined for moving a cell up and down, by default:

screenshot

I simply typed in my preferred combination Ctrl-Shift-Down and Ctrl-Shift-Up in the 'add shortcut' field, and pressed Enter. This is the same in Windows/Mac.

Cheers!

like image 6
Eggroll Of Chaos Avatar answered Oct 13 '22 00:10

Eggroll Of Chaos


This is from the official Jupyter Notebook documentation -

Starting with Jupyter Notebook 5.0, you can customize the command mode shortcuts from within the Notebook Application itself. n”, “n”, “Head to the Help menu and select the Edit keyboard Shortcuts item.n”, “A dialog will guide you through the process of adding custom keyboard shortcuts.n”, “n”, “Keyboard shortcut set from within the Notebook Application will be persisted to your configuration file. n”, “A single action may have several shortcuts attached to it

like image 2
honeybadger Avatar answered Oct 13 '22 01:10

honeybadger