I'd like to have a default indentation size of 2 spaces instead of 4 spaces in my Jupyter notebooks. How can I do this?
Note: this is not a duplicate of How do I change the autoindent to 2 space in IPython notebook, as that question is for (deprecated) IPython notebooks and not (current) Jupyter notebooks.
Use the greater than sign (>) followed by a space, for example: > Text that will be indented when the Markdown is rendered. Any subsequent text is indented until the next carriage return.
The indentation error can occur when the spaces or tabs are not placed properly. There will not be an issue if the interpreter does not find any issues with the spaces or tabs. If there is an error due to indentation, it will come in between the execution and can be a show stopper.
The correct way to do this is in bpirvu's buried answer to How do I change the autoindent to 2 space in IPython notebook:
Just edit ~/.jupyter/nbconfig/notebook.json
. Here is a complete notebook.json
which includes the relevant setting:
{
"CodeCell": {
"cm_config": {
"indentUnit": 2
}
}
}
There's also a more hacky solution that's more prevalent around the web, probably because the Jupyter documentation is lacking on this topic and indentUnit
is only mentioned here: http://jupyter-notebook.readthedocs.io/en/latest/frontend_config.html. This solution is to open your browser's JavaScript console and enter
var cell = Jupyter.notebook.get_selected_cell();
var config = cell.config;
var patch = {
CodeCell:{
cm_config:{indentUnit:2}
}
}
config.update(patch)
which ends up editing ~/.jupyter/nbconfig/notebook.json
for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With