Custom IPython Jupyter Notebook keyboard shortcut to duplicate current line in edit mode
For Microsoft Windows User (pcDefault)
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
For mac user (macDefault)
However if you are a mac user, you need to modify that a bit. You need to change the first line of the code to:
CodeMirror.keyMap.macDefault["Ctrl-Down"] = function(cm){
The rest of the code remains the same. Here is the full snippet.
CodeMirror.keyMap.macDefault["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);
};