问题
In a command line Python session I can press Control-P to retrieve the previously entered line edit it.
How can I perform a similar operation in Jupyter, i.e. carry forward the contents of the previous "In:" block?
回答1:
Looks like Jupyter doesn't have such a feature out of the box, although, you could write your own custom keyboard shortcut using CodeMirror API: https://codemirror.net/doc/manual.html
First you need to create your own custom.js file:
http://jupyter-notebook.readthedocs.io/en/stable/examples/Notebook/JavaScript%20Notebook%20Extensions.html#custom.js
You could try something like this (depending on what you're expecting to get):
CodeMirror.keyMap.pcDefault["Ctrl-P"] = function(cm) {
var selected = Jupyter.notebook.get_selected_cell();
if (!Jupyter.notebook.get_prev_cell(selected)) {
// This is the first cell
return;
}
Jupyter.notebook.select_prev();
Jupyter.notebook.copy_cell();
Jupyter.notebook.select_next();
Jupyter.notebook.paste_cell_replace();
Jupyter.notebook.handle_edit_mode(selected);
}
This will copy the contents of the cell above and insert it into the currently selected cell. You could replace paste_cell_replace() method with paste_cell_above() to create a new cell instead of replacing the contents of the currently selected one.
回答2:
Maybe a lame solution, but you can just copy the cell, right? Press Esc, select the cell (mostly likely the previous one, thus press Up) and press c and v.
来源:https://stackoverflow.com/questions/50311218/jupyter-how-do-i-recall-and-edit-the-previous-in-text