jupyter-notebookauto-indent

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


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?


Solution

  • 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;
                }
            );
        }
    );