How do I change the autoindent to 2 space in IPython notebook

user2826610 picture user2826610 · Sep 28, 2013 · Viewed 25.8k times · Source

I find that developing functions in IPython notebook allows me to work quickly. When I'm happy with the results I copy-paste to a file. The autoindent is 4 spaces, but the coding style for indentation at my company is 2 spaces. How do I change the autoindent to 2 spaces?

Answer

AdamAL picture AdamAL · Jan 27, 2016

The official documentation has an example answering this specific question. This worked for me with IPython 4.

Summary: Paste the following into your browser's javascript console

var cell = Jupyter.notebook.get_selected_cell();
var config = cell.config;
var patch = {
      CodeCell:{
        cm_config:{indentUnit:2}
      }
    }
config.update(patch)

The setting is persisted. You can roll back by exchanging : 2 for : null.