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

9

35

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

Waverley answered 18/6, 2020 at 15:36 Comment(1)
For Jupyter Lab you can edit config files in Settings \ Advanced Settings Editor \ Keyboard Shortcuts. Two commands are relevant: notebook:move-cell-up and notebook:move-cell-down. Instruction is in the default config file.Outlander
P
7

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!

Poplar answered 19/11, 2021 at 21:7 Comment(0)
I
29

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.

Infrasonic answered 22/2, 2021 at 4:46 Comment(3)
This was an easy solution to the general problem, thank youChios
That works! I wonder why this is not a system default in Jupyter...Religieux
I face this problem in VScode, does VScode have Advanced Settings Editor?Muggins
P
7

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!

Poplar answered 19/11, 2021 at 21:7 Comment(0)
P
6

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"
        }
      ]
}
Prickle answered 4/4, 2021 at 16:46 Comment(1)
Same issue here Firefox on Mac OS. This solution worked for me .Thanks Eric!Seville
S
5

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

Superimposed answered 12/3, 2021 at 8:4 Comment(0)
C
2

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
Cheyennecheyne answered 23/11, 2022 at 5:41 Comment(0)
I
1

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
Inapprehensible answered 1/7 at 8:14 Comment(0)
I
0

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
Insulin answered 3/1 at 17:43 Comment(0)
V
0

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.

Vocalize answered 1/7 at 13:46 Comment(3)
About the comment "(version: 6.5.4)", to add some context, this version being referenced is not the prevailing Jupyter Notebook currently. That 6.5.4 version is an end of life branch of the older Notebook tech, now called NbClassic to distinguish it, see here. On the post: "Some versions of Jupyter" is rather vague and likely to cause confusion as presently the ecosystem is rather complex due to Jupyter Notebook version 7+ now being built on the same components as JupyterLab. You added a version ...Walther
<continued> specification in the comment, however, the post would be more specific & thus potentially more helpful, with that in the post if that is indeed the same version being referenced in the text and screenshot.Walther
You already said the version as a comment. I was suggesting it be part of the post, and not simply a comment. It is unclear otherwise for the reasons I stated.Walther
E
-4

Tab + arrow keys works for me in Windows.

Excurved answered 30/7, 2021 at 0:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.