How to add keyboard shortcuts permanently to Jupyter (ipython) notebook?

我的梦境 提交于 2019-11-30 18:45:12

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;
});

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

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.

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"
      }  
    } 
  }   
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!