How can I move a line (or set of selected lines) down or up in jupyter notebook. Are there any shortcuts?
After moving line 3 one line up:
For example ALT+UP
or ALT+DOWN
is for up/down displacement that is used in Eclipse IDE.
This question discusses cell displacement.
-
Same question without answers: [Custom keyboard shortcut for jupyter-notebook and jupyter-lab for moving line up/down](https://stackoverflow.com/q/55692324/7851470) – Georgy Sep 24 '20 at 07:41
-
The easiest way really is to write a swap function for it, but I don't think that's what you're looking for anyway. – Sep 24 '20 at 15:19
5 Answers
TLDR: By following this tutorial, I was able to enable all the sublime-text shortcuts (which includes the moving lines with Ctrl+Up/Ctrl/Down).
I then was able to furthermore, customize the shortcuts by editing the sublime.js
Complete answer:
- Find your jupyter path with
jupyter --config-dir
- Add the following code to
<JUPYTER-PATH>/custom/custom.js
:
require(["codemirror/keymap/sublime", "notebook/js/cell", "base/js/namespace"],
function(sublime_keymap, cell, IPython) {
// setTimeout(function(){ // uncomment line to fake race-condition
cell.Cell.options_default.cm_config.keyMap = 'sublime';
var cells = IPython.notebook.get_cells();
for(var cl=0; cl< cells.length ; cl++){
cells[cl].code_mirror.setOption('keyMap', 'sublime');
}
// }, 1000)// uncomment line to fake race condition
}
);
- You need to find this
codemirror/keymap/sublime.js
(that is required in the code from point 2), for me, it was in<MY-PYTHON-ENVIRONMENT>/lib/python3.8/site-packages/notebook/static/components/codemirror/keymap/sublime.js
- At the end of this file, you can edit the shortcuts, in the
keyMap.pcSublime
section of this file (if you have a PC). - For OP, the lines to edit would be:
swapLineUp
andswapLineDown
I did this with Ubuntu & sublime installed.

- 76
- 5
Since jupyter works with code blocks you can use that shortcut between code blocks as
Ctrl + Shift + -
But this will only works between code cells not in the same code block. I don't think there is a built-in shortcut for that. For more shortcuts you can look here: https://www.dataquest.io/blog/jupyter-notebook-tips-tricks-shortcuts/

- 515
- 5
- 14
You can select the line with mouse in jupyter notebook. Drag it up or down before the line where you want to move it. Press > button on keyboard while your text is still selected.
This will bring the cursor to end of selected line, but before the text of line being replaced. Press Enter.

- 4,393
- 2
- 9
- 22
There's no such a shortcut in Jupyter, however, you can manage your own shorcuts Go to Help
> Edit keyboard Shortcuts
and start setting your commands.
There's some good info on this other post which will lead you to manage your profile's notebook.json file and so on.
Hope this was useful ;)
Edit: typo

- 196
- 6