将键盘快捷键映射到 Jupyter Lab 中的代码片段

Sra*_*ffa 7 jupyter-irkernel jupyter-lab

有谁知道是否有办法将代码片段绑定到 Jupyter Lab 中的键盘快捷键?例如,在 R Studio 中,您可以使用Ctrl+ Shift+快速M编写管道运算符 ( %>%),我已经习惯了该功能,因此我想复制它。

我查看了“设置”下的“键盘快捷键”菜单,但我不确定如何使用 JSON 模式来编写这样的覆盖(如果甚至可以从那里开始),并且文档也不是很清楚。

kra*_*ski 6

在 JupyterLab 2.1+ 中,您可以使用以下命令配置 R 的键盘快捷键:

{
    "shortcuts": [
        {
            "command": "apputils:run-first-enabled",
            "selector": "body",
            "keys": ["Alt -"],
            "args": {
                "commands": [
                    "console:replace-selection",
                    "fileeditor:replace-selection",
                    "notebook:replace-selection",
                ],
                "args": {"text": "<- "}
            }
        },
        {
            "command": "apputils:run-first-enabled",
            "selector": "body",
            "keys": ["Accel Shift M"],
            "args": {
                "commands": [
                    "console:replace-selection",
                    "fileeditor:replace-selection",
                    "notebook:replace-selection",
                ],
                "args": {"text": "%>% "}
            }
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Accel表示Ctrl在 Linux 和 Windows 上,或Command在 Mac 上。

只需转到Advanced Settings Editor-> Keyboard Shortcuts,粘贴并保存:

插图

JupyterLab 存储库中的相关问题:#4519#7908#10114

或者,对于更高级的功能,例如特定于内核的快捷方式或自动填充,您可以使用jupyterext-text-shortcuts扩展。


Pau*_*ong 3

好吧,我对 Jupyter Lab 没有一个好的答案,但对于好的旧 Jupyter Notebook,只需运行下面的单元格即可给你你想要的东西:

IRdisplay::display_javascript("Jupyter.keyboard_manager.edit_shortcuts.add_shortcut('Ctrl-Shift-M', {
    help : 'add pipe symbol',
    help_index : 'zz',
    handler : function (event) {
        var target = Jupyter.notebook.get_selected_cell()
        var cursor = target.code_mirror.getCursor()
        var before = target.get_pre_cursor()
        var after = target.get_post_cursor()
        target.set_text(before + ' %>% ' + after)
        cursor.ch += 5
        target.code_mirror.setCursor(cursor)
        return false;
    }}
);")
Run Code Online (Sandbox Code Playgroud)