Jupyter notebook hacks
Aug 27, 2017 · 1 min read
Run the following code blocks in a Jupyter notebook cell, to get awesome keyboard shortcuts.
Use Ctrl+⇧+⏎ for a new line and unindent. Gives you a fast option to move out of a loop, function or class definition …
# Activate Jupyter %%javascript magic %%javascript # bind ctrl-shift-return to new line and unindent Jupyter.keyboard_manager.edit_shortcuts.add_shortcut('ctrl-shift-enter', { help : 'New line and unindent', help_index : 'zz', handler: function(env) { var cm=env.notebook.get_selected_cell().code_mirror; cm.execCommand('newlineAndIndent'); cm.execCommand('indentLess'); return false; }} );⌘+⇧+d to duplicate the current line. This is super helpful to quickly add altered versions of code.
# Activate Jupyter %%javascript magic %%javascript # Bind cmd-shift-d to duplicate line of code Jupyter.keyboard_manager.edit_shortcuts.add_shortcut('cmd-shift-d', { help : 'Duplicate current line', help_index : 'zz', handler: function(env) { var cm=env.notebook.get_selected_cell().code_mirror; // get a position of a current cursor in a current cell var current_cursor = cm.doc.getCursor(); // read a content from a line where is the current cursor var line_content = cm.doc.getLine(current_cursor.line); cm.execCommand('goLineEnd'); cm.execCommand('newlineAndIndent'); cm.execCommand('indentLess'); cm.doc.replaceSelection(line_content); cm.doc.setCursor(current_cursor.line + 1, current_cursor.ch); return false; }} );Get the latest posts delivered right to your inbox
Originally published at volderette.de on August 27, 2017.
