How to add a keyboard shortcut to insert a code snippet
Inspired by https://stackoverflow.com/a/51719689/9201239
and some help from @stas
If you first enter a cell with either code mode or markdown mode, like the following
Then you press Ctrl + Shift + M
, you will get the following
To make this happening, you just need to
- go to
custom.js
whose path can be found by running
echo $(jupyter --config-dir)/custom/custom.js
- add the following codes into
custom.js
and save.
Jupyter.keyboard_manager.edit_shortcuts.add_shortcut('Ctrl-Shift-M', {
help : 'add details drop',
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 + '[/details][details=""]' + after)
cursor.ch += 20 // where to put your cursor
target.code_mirror.setCursor(cursor)
return false;
}}
);
- finally, go back to Jupyter Notebook and refresh your page, and you are ready to go.