如何将键盘快捷键永久添加到 Jupyter (ipython) 笔记本?

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

我有以下快捷方式配置,在 运行 它在 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;                   
    }
  });

如何设置 Jupiter notebook 在启动时自动进行初始化?

我尝试将相同的代码(没有 %%javascript)添加到 C:\Users\<username>\.ipython\profile_default\static\custom\custom.js,但没有成功。

我只有一个配置文件,使用 ipython profile create、Python 3.3、Windows 7 创建。

提前致谢。

custom.js 是此代码的正确位置。尝试按如下方式包装它(不要忘记块结束前的 return true):

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

    return true;
});

使用 nbextensions 轻松添加热键

  1. 安装nbextensions.
    pip install jupyter_contrib_nbextensions
  2. 然后启动 jupyter notebook。
  3. 介绍页面将有一个名为 nbextensions 的新选项卡,单击它并启用键盘快捷键编辑器。
  4. 现在打开任何笔记本点击帮助>键盘快捷键
  5. 每个快捷方式旁边都会有一个铅笔图标,如果您单击它,然后您可以将快捷方式设置为任何您想要的。

Jupyter notebook 的新版本中(使用 pip install --upgrade notebook 或使用 conda conda upgrade notebook 更新它),您可以从笔记本本身自定义它们。

执行此操作帮助 -> 编辑键盘快捷键

1.要更改命令模式快捷方式: 参考 Salvador 的回答

2。更改编辑模式快捷方式:

编辑文件,~/.jupyter/nbconfig/notebook.jsonhttps://jupyter-notebook.readthedocs.io/en/stable/extending/keymaps.html

中所述

例如,将执行代码的 control-enter 快捷方式替换为 macOS 上的 command-enter 后,文件如下所示:

{
  "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"
      }  
    } 
  }   
}