问题
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