Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Jupyter notebook, how to change auto indent to tab instead of 4 spaces

I just started using jupyter notebook. Google search didn't help. Thanks!

Update: Quick Summary of answers

Running the following code in a cell before you start a ipython file got the task done for me. One problem is that we have to run this everytime for each file.


    %%javascript

    // apply setting to all current CodeMirror instances
    IPython.notebook.get_cells().map(
        function(c) {  return c.code_mirror.options.indentWithTabs=true;  }
    );

    // make sure new CodeMirror instances created in the future also use this setting
    CodeMirror.defaults.indentWithTabs=true;

like image 754
Varun Janga Avatar asked Nov 29 '16 09:11

Varun Janga


2 Answers

If you run this javascript code in a cell it should allow you to insert hard tabs:

%%javascript

IPython.tab_as_tab_everywhere = function(use_tabs) {
    if (use_tabs === undefined) {
        use_tabs = true; 
    }

    // apply setting to all current CodeMirror instances
    IPython.notebook.get_cells().map(
        function(c) {  return c.code_mirror.options.indentWithTabs=use_tabs;  }
    );
    // make sure new CodeMirror instances created in the future also use this setting
    CodeMirror.defaults.indentWithTabs=use_tabs;

    };

IPython.tab_as_tab_everywhere()

It works for me. Source = http://pirsquared.org/blog/indenting-tabs.html

like image 141
AlexG Avatar answered Nov 07 '22 00:11

AlexG


In JupyterLab 2.0+:

enter image description here

For those visually impaired: Settings > Text Editor Indentation > Indent with Tab

like image 22
Kalanos Avatar answered Nov 07 '22 00:11

Kalanos