12

I have the following configuration for shortcuts, that works after running it in the cell of Jupiter notebook:

%%javascript


IPython.keyboard_manager.command_shortcuts.add_shortcut('ctrl-q', {
    help: 'Clear all output',               // This text will show up on the help page (CTRL-M h or ESC h)
    handler: function (event) {             // Function that gets invoked
        if (IPython.notebook.mode == 'command') {
            IPython.notebook.clear_all_output();
            return false;
        }
        return true;                   
    }
  });

How can I setup Jupiter notebook to make this initialization automatically on startup?

I tried adding the same code (without %%javascript) to C:\Users\<username>\.ipython\profile_default\static\custom\custom.js but it didn't work.

I have only one profile, created with ipython profile create, Python 3.3, Windows 7.

Thanks in advance.

James Draper
  • 5,110
  • 5
  • 40
  • 59
Apogentus
  • 6,371
  • 6
  • 32
  • 33

4 Answers4

10

In the new version of Jupyter notebook (update it either with pip install --upgrade notebook or if you use conda conda upgrade notebook), you can customize them from the notebook itself.

To do this Help -> Edit keyboard shortcuts

enter image description here

Salvador Dali
  • 214,103
  • 147
  • 703
  • 753
  • 3
    Note this doesn't solve keyboard shortcuts for editing mode, only command mode. Thus while `enter command mode` is ostensibly available, it is not actually set by this interface. – mpacer May 17 '17 at 05:13
  • 3
    This is also not persistent – Celdor Jun 20 '17 at 12:11
  • 2
    @Celtor it was persistent for me on Jupyter 4.4.0, was saved under `~/.jupyter/nbconfig/notebook.json`. – Ciro Santilli OurBigBook.com Nov 20 '17 at 09:12
  • 1
    Doesn't work for me. I am trying to assign a shortcut to `restart kernel and run all cells` but once I save from the dialog and then reopen the dialog the shortcut is gone. It doesn't work at all – Jay Chakra Jul 12 '18 at 18:40
  • I have used "Edit Keyboard Shortcuts" and did some customization quite a few times, but I cannot find them in any of Jupiter's customization files ('custom.js' or 'notebook.json'). Where are they stored ??? – Apostolos May 14 '19 at 10:20
  • 1
    @JayChakra I'm probably late, but I had the same issue and here are things that must be noticed: 1- your choice of key combinations must be v valid according to the explanations exactly at the bottom of where you assign keys 2- it must not be already used for another command (must not become red when you type it in) 3- you have to hit Enter and see it gets added to the left side of it. – aderchox May 30 '19 at 11:48
8

custom.js is the correct place for this code. Try wrapping it as follows (don't forget the return true before the end of the block):

$([IPython.events]).on("app_initialized.NotebookApp", function () {
    <your code>

    return true;
});
Dmitri
  • 757
  • 1
  • 7
  • 15
  • 9
    Do you really believe that "" is enough information for someone to create a keyboard shortcut using your method?! – Apostolos May 14 '19 at 10:17
6

1. For changing command mode shortcuts: refer Salvador's answer

2. For changing edit mode shortcuts:

Edit the file, ~/.jupyter/nbconfig/notebook.json as explained on https://jupyter-notebook.readthedocs.io/en/stable/extending/keymaps.html

For example, after replacing the control-enter shortcut to execute code, with command-enter on macOS, the file looks like this:

{
  "Notebook": {
    "Toolbar": true,
    "Header": true
  },
  "Cell": {
    "cm_config": {
      "lineNumbers": true
    }
  },
  "keys": {
    "command": {
      "unbind": [
        "ctrl-enter"
      ],
      "bind": {
        "cmdtrl-enter": "jupyter-notebook:run-cell"   
      }
    }, 
    "edit": {
      "unbind": [
        "ctrl-enter"
      ],
      "bind": {
        "cmdtrl-enter": "jupyter-notebook:run-cell"
      }  
    } 
  }   
} 
Sachit Nagpal
  • 486
  • 4
  • 7
  • 1. Doesn't this work both command and edit modes? 2. Doesn't Salvador Dali's answer above just edit this file via the GUI? – flow2k Oct 30 '20 at 06:14
  • Salvador's answer only changed command mode shortcuts, not edit mode – Sachit Nagpal Oct 31 '20 at 07:23
  • @ Sachit Nagpal, thanks for the response. Is there documentation to support what you say? I see `"edit": ` in your code snippet above, and I tried with version `6.1.4` and it seems to configure `edit` mode keymap as well. – flow2k Oct 31 '20 at 22:17
  • My point is that editing `~/.jupyter/nbconfig/notebook.json` should work for both command and edit modes. – flow2k Oct 31 '20 at 22:19
  • You can edit the file accordingly. Do not bind/unbind anything under the edit key then and only add bindings under the command key if you want to only edit the command mode shortcut. – Sachit Nagpal Nov 07 '20 at 04:45
4

Adding hotkeys the easy way with nbextensions

  1. Install nbextensions.
    pip install jupyter_contrib_nbextensions
  2. Then launch jupyter notebook.
  3. The the intro page will have a new tab called nbextensions click it and enable Keyboard Shortcut Editor.
  4. Now open any notebook click help>keyboard shortcuts
  5. Each shortcut will have a pencil icon next to it if you click on it then you can set the shortcut to whatever you want.
James Draper
  • 5,110
  • 5
  • 40
  • 59