在Jupyter Lab中将键盘快捷方式映射到代码段 [英] Map keyboard shortcut to code snippet in Jupyter Lab

查看:113
本文介绍了在Jupyter Lab中将键盘快捷方式映射到代码段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道Jupyter Lab中是否可以将代码段绑定到键盘快捷方式吗?例如,在R Studio中,您可以使用 Ctrl + Shift + M 快速编写管道运算符(%>%),而我习惯了该功能,所以我想复制它.

Does anyone know if there a way to bind a code snippet to a keyboard shortcut in Jupyter Lab? For example in R Studio you can use Ctrl+Shift+M to write the pipe operator (%>%) quickly and I got used to that functionality so I would like to replicate it.

我查看了设置"下的键盘快捷键"菜单,但是我不确定如何使用JSON模式编写这样的Override(如果可能的话),并且文档也不十分清楚.

I looked at the Keyboard Shortcut menu under Settings but I'm not sure how to use the JSON schema to write such an Override (if it is even possible from there), and the documentation wasn't very clear.

推荐答案

好的,我对Jupyter Lab没有很好的答案,但是对于旧的Jupyter笔记本,只需运行下面的单元格就可以为您提供所需的内容:

OK I don't have a good answer for Jupyter Lab, but for good old jupyter notebook, just ran the below cell would give you what you want:

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

这篇关于在Jupyter Lab中将键盘快捷方式映射到代码段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆