[PYTHON] Initial setting of Jupyter Notebook for Vim lovers ・ Exit with jj (jupyter-vim-binding)

Preface

I want to do what the title says. Our initial development environment doesn't contain anything, and jj tends to be displayed unknowingly (I think there is vim). Since it is personally desirable that Jupyter also has a basic vim environment, the procedure for setting this is described. The company environment is linux, but since I am writing an article on my home PC with a clean installation, it is about macOS. However, it is almost the same.

Environment / preparation

Method

Introduction of jupyter-vim-binding

Basically, I think this is fine. I think there are others, but this time I will write with this. First, install jupyter notebook with pip. I use it anyway, so pandas too.

pip install jupyter
pip install pandas

Follow the procedure written on github for brain death.

mkdir -p $(jupyter --data-dir)/nbextensions
cd $(jupyter --data-dir)/nbextensions
git clone https://github.com/lambdalisue/jupyter-vim-binding vim_binding
jupyter nbextension enable vim_binding/vim_binding

For the time being, if you open jupyter at this point, you will see something like the following.

cd
jupyter notebook
スクリーンショット 2020-04-30 15.16.52.png

This is the home directory, but for the time being, if you select python3 from New in the upper right and open it, スクリーンショット 2020-04-30 15.18.41.png

Untitled.ipynb is created with this. When it is cream, it is in normal mode, and press i to enter insert mode. See the original page for details. This.

If you do not set it, you will not be able to exit the insert mode with jj and get angry, so edit the key mapping.

Edit key mapping

In this article, edit with nvim (please use an appropriate editor). It seems that jupyter settings are placed in ~ / .jupyter. Create or edit custom.js under custom in it.

nvim ~/.jupyter/custom/custom.js

When the editor opens, save the following: This is the officially written setting, except for the setting to return to normal mode with jj.

// Configure CodeMirror Keymap
require([
  'nbextensions/vim_binding/vim_binding',   // depends your installation
], function() {
  CodeMirror.Vim.map("jj", "<Esc>", "insert"); // jj setting!
});

// Configure Jupyter Keymap
require([
  'nbextensions/vim_binding/vim_binding',
  'base/js/namespace',
], function(vim_binding, ns) {
  // Add post callback
  vim_binding.on_ready_callbacks.push(function(){
    var km = ns.keyboard_manager;
    // Allow Ctrl-2 to change the cell mode into Markdown in Vim normal mode
    km.edit_shortcuts.add_shortcut('ctrl-2', 'vim-binding:change-cell-to-markdown', true);
    // Update Help
    km.edit_shortcuts.events.trigger('rebuild.QuickHelp');
  });
});

Now you can edit it quite comfortably. You should understand if you use it.

in conclusion

In the company environment, it was difficult to install something due to security, but I managed to get it because I could go to git. When it becomes another environment, I will do it like this article for the time being. For vimmer, at least the movement of jkhl is a matter of life and death, so I think it is better to set only jupyter-vim-binding.

Recommended Posts

Initial setting of Jupyter Notebook for Vim lovers ・ Exit with jj (jupyter-vim-binding)
Data analysis for improving POG 2 ~ Analysis with jupyter notebook ~
Fill the browser with the width of Jupyter Notebook
Recommendation of Jupyter Notebook, a coding environment for data scientists
Shortcut key for Jupyter notebook
Using Graphviz with Jupyter Notebook
Use pip with Jupyter Notebook
Use Cython with Jupyter Notebook
Play with Jupyter Notebook (IPython Notebook)
[Linux Nginx] Command collection used for initial setting of Web server
Git management of Jupyter notebook (ipynb) differences in easy-to-read with JupyterLab
Output log file with Job (Notebook) of Cloud Pak for Data