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

- 1,571
- 2
- 14
- 25
-
2Yes, I have met this, but this totally unacceptable if not outrageous. Jupyter Notebook is the only editor I have met in my life in whuch you cannot set your own TAB space! Besides, it's such a simple and common request! – Apostolos May 13 '19 at 11:32
-
1To see the changes you also need to reload the notebook page in the browser – Giuseppe Galano Jul 29 '21 at 08:47
-
The link in the answer is no longer valid (2022-12) – Kolban Dec 23 '22 at 16:02
From the official documentation for CodeMirror Code Cells:
- Open an Ipython Notebook
- Create a Code Cell e.g. by pressing
b
- Open your browser’s JavaScript console and run the following
snippet:
var cell = Jupyter.notebook.get_selected_cell();
var config = cell.config;
var patch = {
CodeCell:{
cm_config:{indentUnit:2}
}
}
config.update(patch)
- Reload the notebook page in the browser e.g. by pressing
F5
This will fix it permanently. I assume this works only on recent versions, not sure though!

- 8,884
- 7
- 41
- 50
-
1This procedure works on my Windows 10 Chrome. Jupyter notebook version: 4.2.3. Thank you! – jsung8 Jul 01 '17 at 07:25
-
1
-
1I did the above, i.e. copy-pasted the code into Firefox browser console, it was accepted but it also issued an error: "ReferenceError: Jupyter is not defined"! Oh comeon, it's already an offbeat solution. It doesn't work either! – Apostolos May 13 '19 at 11:37
-
1@Apostolos you have to open the Console from the browser tab that is displaying your notebook. You interact with the site that is displayed. See f.e. https://developer.mozilla.org/en-US/docs/Tools/Web_Console – Frank Haubenreisser Nov 01 '19 at 17:40
-
Thanks @Frank for your delayed answer. You talk about learning how to drive. However, the question is very specific: it's how to pull the seat of the car further back. (All this is figurative speaking, of course.) Anyway I have solved the problem since then. – Apostolos Nov 04 '19 at 10:13
AdamAL's answer is correct. It worked for me.
However it only changes the indentation in the Jupyter Notebook and leaves the indentation in the Jupyter Editor unaffected.
A more direct way to change the indentation is to directly edit the Jupyter config files in the .jupyter/nbconfig
directory. This directory contains 2 files:
edit.json
notebook.json
The option you must set in either one is indentUnit
. Here is the content of my Jupyter config files:
edit.json:
{
"Editor": {
"codemirror_options": {
"indentUnit": 2,
"vimMode": false,
"keyMap": "default"
}
}
}
notebook.json:
{
"CodeCell": {
"cm_config": {
"indentUnit": 2
}
}
}
With this approach I've set the default indentation to 2 in both the Jupyter Notebook and the Jupyter Editor.

- 791
- 7
- 8
-
The above configuration files also work for changing indentation to use tab characters. Use "indentWithTabs": true in cm_config and codemirror_options. Other suggestions were either not permanent or didn't work with Jupyter 4.1.1/iPython 5.1.0 – iggie Nov 22 '16 at 14:45
-
Adding to this response: * Configuration directory can be found with `jupyter --config_dir` * If the directory and `nbconfig/` and the files `edit.json` and `notebook.json` don't exist, you can create them with the contents described by bpirvu. – Jan Aug 02 '17 at 19:29
-
The indentWithTabs option should not be set, as iggie suggests, at least not for python code. It is against widely used convention and it can lead to indentation errors. PEP-8 also believes you should use spaces in python. – mit Oct 17 '18 at 09:50
-
-
-
4
-
2
Based on this question and the options found here:
In your custom.js file (location depends on your OS) put
IPython.Cell.options_default.cm_config.indentUnit = 2;
On my machine the file is located in ~/.ipython/profile_default/static/custom
Update:
In IPython 3 the plain call does not work any more, thus it is required to place the setting within an appropriate event handler. A possible solution could look like
define([
'base/js/namespace',
'base/js/events'
],
function(IPython, events) {
events.on("app_initialized.NotebookApp",
function () {
IPython.Cell.options_default.cm_config.indentUnit = 2;
}
);
}
);
-
This approach seems to be explicitly discouraged [1] now: "Note that you will find legacy extensions on the internet that do not define load_ipython_extension and rely on IPython's Events, and Custom.js. While this does work for the time being, these extensions will break in the future and are subject to race conditions." [1]:https://carreau.gitbooks.io/jupyter-book/content/notebook-extensions.html – AdamAL Jan 27 '16 at 21:22
If you use jupyterlab, there seems to be an easier way:
1) Click jupyterlab menu Settings > Advanced Setting Editor
2) Click "Notebook" on the left hand pane, make sure you are on "Raw View"
3) On the right pane, under "User Overrides", enter this:
{
"codeCellConfig": {
"tabSize": 2
}
}
If you look at the System Defaults, that will give you hint on whats overridable and you can repeat this for other settings.
I tried this on Google Platform AI Notebook which uses Jupyterlab.

- 3,649
- 2
- 30
- 50
I believe this is now best wrapped in a event handler to load once per notebook load:
$([IPython.events]).on('app_initialized.NotebookApp', function(){
IPython.CodeCell.options_default['cm_config']['indentUnit'] = 2;
});

- 2,168
- 15
- 28
-
1Even though I'm using juypter this worked for me (added the line at the bottom of ~/.ipython/profile_default/static/custom/custom.js – dranxo Oct 28 '15 at 18:49
-
2I added this to `~/.jupyter/custom/custom.js` (after creating the `custom/` dir) – Matt Mar 08 '16 at 23:40
In addition to adding
IPython.Cell.options_default.cm_config.indentUnit = 2;
to your custom.js
file as suggested by Jakob, be sure to clear your browser cache as well before restarting ipython notebook!
Also, you may have to first create the ~/.config/ipython/profile_default/static/custom/
directory (use echo $(ipython locate default)
to find your default directory) before adding the custom.js
file.

- 21
- 2