Learn how to use sublime text shortcuts in Jupyter Notebook.
You can use useful features in sublime text such as duplicate lines, multiple selections, and simultaneous editing of multiple lines. You can also change the shortcut assignments.
An editor that allows you to save Python code and execution results together. You can get a feel for the atmosphere on the top page of the official website: Project Jupyter | Home
Jupyter notebook has functions such as executing chunks of code (cells) and adding cells. How to customize shortcuts for these features is officially detailed: Customize keymaps — Jupyter Notebook 6.0.3 documentation .html)
You can customize it by creating a file for shortcut customization as shown below.
json:~/.jupyter/nbconfig/notebook.json
{
"keys": {
"command": {
"bind": {
"G,G,G":"jupyter-notebook:restart-kernel-and-run-all-cells"
}
}
},
}
You can also intuitively change the shortcut in the GUI.
However, I don't think there is much explanation about the shortcuts used for editing code in the editor.
This article describes how to customize editor shortcuts.
You can customize the shortcuts for editing code by the following methods, for example, you can use the shortcut of sublime text.
Some JavaScript files loaded by jupyter notebook are intended to be customized by the user. That is custom.js.
It is not placed by default, but will be loaded automatically if it exists.
Create a file in ~ / .jupyter / custom / custom.js
. Since the location may be different depending on the OS,
import jupyter_core
jupyter_core.paths.jupyter_config_dir()
#This result is'~/.jupyter'Should be.
#In that case,'~/.jupyter/custom/custom.js'Is read.
Please check with.
Write the following in this custom.js. In the JavaScript of jupyter notebook, requirejs is used for module management, so write it so that it matches the syntax.
Javascript:.jupyter/custom/custom.js
define([
"custom/js_required/import-sublime-keybindings",
"base/js/events"
], function(keybindings, events) {
"use strict";
keybindings.bindSublimeKeymap();
});
In an external file, write the shortcut settings as shown below and load them with custom.js.
Javascript:.jupyter/custom/js_required/import-sublime-keybindings.js
define([
'base/js/namespace',
'notebook/js/cell',
'codemirror/lib/codemirror',
'codemirror/keymap/sublime'
], function(IPython, cell, CodeMirror) {
"use strict";
var bindSublimeKeymap = function() {
var map = CodeMirror.keyMap.sublime;
var notebook = IPython.notebook;
if (!notebook) return;
//Function to invalidate an existing shortcut
var deleteIfExist = function(strCommand) {
if (map[strCommand]) {
delete map[strCommand];
}
};
//Now set the keymap to sublime text
cell.Cell.options_default.cm_config.keyMap = 'sublime';
// Cmd-Disable Enter shortcut
deleteIfExist("Cmd-Enter");
//Prepare your own shortcuts that are not provided by default in sublime text
map["Shift-Cmd-D"] = "deleteLine";
map["Cmd-D"] = "duplicateLine";
map["Alt-W"] = "wrapLines";
map["Cmd-B"] = "selectNextOccurrence";
map["Shift-Cmd-M"] = "selectBetweenBrackets";
map["Alt-Up"] = "swapLineUp";
map["ALt-Down"] = "swapLineDown";
//The shortcut is set in the new cell.
//Do the following to apply to existing cells
var cells = IPython.notebook.get_cells();
var numCells = cells.length;
for (var c = 0; c < numCells; c++) {
cells[c].code_mirror.setOption('keyMap', 'sublime');
}
};
return { bindSublimeKeymap: bindSublimeKeymap };
});
Now restart your Jupyter notebook and the shortcut should apply.
Recommended Posts