4

I am trying to add the Cntl+K and Cntl+J shortcuts to move cells up and down quickly. I viewed the issue on Github here for adding the shortcuts and found what looked to be a workable answer:

"For those (like me) who liked this shortcut, add this to your ~/.ipython/profile_default/static/custom/custom.js:

$([IPython.events]).on("app_initialized.NotebookApp", function () {

IPython.keyboard_manager.command_shortcuts.add_shortcut('ctrl-k', function (event) {
    IPython.notebook.move_cell_up();
    return false;
});

IPython.keyboard_manager.command_shortcuts.add_shortcut('ctrl-j', function (event) {
    IPython.notebook.move_cell_down();
    return false;
});

});

"

But my users/{my name}/.ipython/profile_default directory did not have a static folder. I tried adding the missing folders and custom.js file, and reopened Anaconda prompt, but this did not add the missing shortcuts.

Another answer had the same issue:

"Use the following:

$ cat ~/.jupyter/custom/custom.js

define(["base/js/namespace"], function(Jupyter){
    console.info('Binding Ctrl-J/K to move cell up/down');
    Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Ctrl-k','jupyter-notebook:move-cell-up');
    Jupyter.keyboard_manager.command_shortcuts.add_shortcut('Ctrl-j','jupyter-notebook:move-cell-down');
});

"

This answer also did not work (adding the missing folder and custom.js file did not work).

1 Answer 1

7

As proposed in the official doc (got with the "Help>Notebook" menu action), you could try first in a live notebook. The browser javascript console helps too.

I tried:


%%javascript

IPython.keyboard_manager.command_shortcuts.add_shortcut('Ctrl-k','jupyter-notebook:move-cell-up');
// replacing IPython with Jupyter should work as well:
Jupyterkeyboard_manager.command_shortcuts.add_shortcut('Ctrl-j','jupyter-notebook:move-cell-down');

It works but, just as when clicking on the corresponding toolbar button, the console warns about deprecation in favor of IPython.notebook.move_selection_up() .

The string "jupyter-notebook:move-cell-up" refers to the same action.

So I suppose a reasonnable thing to do is to redefine it from scratch:


IPython.keyboard_manager.command_shortcuts.add_shortcut('Ctrl-k', {
    help : 'move up selected cells',
    help_index : 'jupyter-notebook:move-selection-up',
    handler : function (event) {
        IPython.notebook.move_selection_up();
        return false;
    }}
);

IPython.keyboard_manager.command_shortcuts.add_shortcut('Ctrl-j', {
    help : 'move down selected cells',
    help_index : 'jupyter-notebook:move-selection-down',
    handler :  function (event) {
        IPython.notebook.move_selection_down();
        return false;
    }}
);

After executing the notbook cell (or the code in your browser console), it should be active and you can experiment with it.

Once happy, check the path of your jupyter profile with !jupyter --config, and from there you'd know where to copy your code : <profile>/static/custom/custom.js so that it would be active in next jupyter sessions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.