Burhan
Burhan

Reputation: 423

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?

Upvotes: 39

Views: 53229

Answers (9)

Victor deMatos
Victor deMatos

Reputation: 963

Some versions of Jupyter come not with a few short cuts, you can then add them yourself. Any OS (my current version (version: 6.5.4)):

enter image description here

Over, and out.

Upvotes: 0

Waqas Ahmed
Waqas Ahmed

Reputation: 43

For Jupyter Notebook on Ubuntu:

  1. Click the cell you want to move up/down
  2. Press Escape key
  3. Press Alt + Up-Arrow for moving cell up and Alt + Down-Arrow for moving cell down

Upvotes: 4

adobelis
adobelis

Reputation: 126

On MacOS + Chrome:

In the Jupyter menu, click: View -> "Activate Command Palette"

Then search for "Move" -- you'll see the relevant command shortcut. For my unmodified install of Jupyter this is:

  • Move up: ctl + shift + up-arrow
  • Move down: ctl + shift + down-arrow

Upvotes: 0

ABHISHEK D
ABHISHEK D

Reputation: 133

Steps to add a shortcut to move cells up or down

  1. Open any Jupyter Notebook
  2. click on Help
  3. Click on Edit Keyboard Shortcuts
  4. Find move cell down and tap on 'add a shortcut', give any shortcut like "D" for example
  5. Click on the '+' icon
  6. Do the same for move cell up, you can give "U" for this
  7. click on Ok Now use these shortcuts to move your cells above or below

Upvotes: 3

David
David

Reputation: 400

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.

Upvotes: 29

Eggroll Of Chaos
Eggroll Of Chaos

Reputation: 106

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!

Upvotes: 8

kartik sama
kartik sama

Reputation: 9

Tab + arrow keys works for me in Windows.

Upvotes: -4

Eric Lief
Eric Lief

Reputation: 61

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"
        }
      ]
}

Upvotes: 6

honeybadger
honeybadger

Reputation: 1575

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

Upvotes: 5

Related Questions