Map keyboard shortcut to code snippet in Jupyter Lab

感情迁移 提交于 2020-02-03 10:13:54

问题


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.

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.


回答1:


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


来源:https://stackoverflow.com/questions/49440087/map-keyboard-shortcut-to-code-snippet-in-jupyter-lab

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