Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom IPython Notebook keyboard shortcut to duplicate current line in edit mode

Tags:

In the IPython Notebook environment, it is possible to define custom keyboard shortcuts using the IPython Javascript API. Using the %%javascript magic, one may write a javascript within IPython's interactive console as follows (example described here):

%%javascript  IPython.keyboard_manager.command_shortcuts.add_shortcut('r', {     help : 'run cell',     help_index : 'zz',     handler : function (event) {         IPython.notebook.execute_cell();         return false;     }} ); 

I'd like to write a javascript that creates a shortcut during edit mode that binds Ctrl-Alt-Down to the action of 'duplicate current line'---that is, move the cursor to the start of the current line, select the line, copy the line, return, paste. Essentially, I want to emulate the keyboard shortcut of Eclipse, or Ctrl-d in Notepad++, or C-a C-SPACE C-n M-w C-y in Emacs. The javascript file will take the form of the following:

%%javascript  IPython.keyboard_manager.edit_shortcuts.add_shortcut('ctrl-alt-down', {     help : 'run cell',     help_index : 'zz',     handler : function (event) {         [Code that duplicates the line];         return false;     }} ); 

though my attempts suggest 'ctrl-alt-down' is the incorrect way to represent the shortcut sequence, and I can't find any documentation for the keyboard_manager.

I'd rather not go with an (e.g.,) AutoHotKey solution since I want to restrict this shortcut to the edit mode of IPython Notebook.

like image 663
brorgschlr Avatar asked Mar 24 '15 23:03

brorgschlr


People also ask

How do you duplicate a line in Jupyter Notebook?

To duplicate a line in Jupyter-notebook on a Mac, the hotkey is cmd + shift + D .

How do I edit keyboard shortcuts in Jupyter Notebook?

Starting with Jupyter Notebook 5.0, you can customize the command mode shortcuts from within the Notebook Application itself. Head to the ``Help`` menu and select the ``Edit keyboard Shortcuts`` item. A dialog will guide you through the process of adding custom keyboard shortcuts.

Which keyboard shortcut in Jupyter can you use to run the current cell and then select the cell below?

Shortcuts in both modes: Shift + Enter run the current cell, select below.

How do you use keyboard shortcuts in Jupyter Notebook?

Press 'Enter' to change the cell into the edit mode. You can modify the command mode shortcuts by using the help menu. Click on the 'Help' and choose the 'Edit keyboard shortcuts' option. The following interface shows in the Jupyter notebook where you can define new keyboard shortcuts.


1 Answers

Step1.

Create a new JS file under ~/.jupyter/custom/custom.js if it does not exist and add the next code:

/** * * Duplicate a current line in the Jupyter Notebook * Used only CodeMirror API - https://codemirror.net * **/ CodeMirror.keyMap.pcDefault["Ctrl-Down"] = function(cm){      // 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);      // go to the end the current line     CodeMirror.commands.goLineEnd(cm);      // make a break for a new line     CodeMirror.commands.newlineAndIndent(cm);      // filled a content of the new line content from line above it     cm.doc.replaceSelection(line_content);      // restore position cursor on the new line     cm.doc.setCursor(current_cursor.line + 1, current_cursor.ch); }; 

Step 2.

Restart Jupyter

Result

enter image description here

Tested in a next environment

wlysenko@wlysenko-Aspire ~ $ google-chrome --version Google Chrome 53.0.2785.116  wlysenko@wlysenko-Aspire ~ $ jupyter --version 4.1.0 wlysenko@wlysenko-Aspire ~ $ uname -a Linux wlysenko-Aspire 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux 
like image 131
PADYMKO Avatar answered Sep 28 '22 05:09

PADYMKO