[JAVA] Customize Jupyter notebook shortcuts to look like sublime text

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.

What is Jupyter Notebook?

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

There are two types of Jupyter Notebook shortcuts: "Editor Shortcut" and "Command Execution" shortcuts.

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. 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.

Customize JavaScript behavior in jupyter notebook with custom.js

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

Customize Jupyter notebook shortcuts to look like sublime text
Introduced Jupyter Notebook to CentOS 7
How to use Jupyter Notebook
Added achievement function to Sublime Text
Easy to use Jupyter notebook (Python3.5)
Markdown to get Jupyter notebook results to Qiita
I want to blog with Jupyter Notebook
How to execute commands in jupyter notebook
How to use jupyter notebook with ABCI
Jupyter Notebook Basics of how to use
How to use Jupyter notebook [Super Basic]
[Jupyter Notebook / Lab] 3 ways to debug on Jupyter [Pdb]
Plugin to add variable symbols (Sublime Text) Description
Jupyter Notebook Settings-How to use (EC2 Amazon Linux 2)