Varun Janga
Varun Janga

Reputation: 136

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;

Upvotes: 10

Views: 24049

Answers (4)

Kermit
Kermit

Reputation: 5992

In JupyterLab 2.0+:

enter image description here

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

Upvotes: 3

user202729
user202729

Reputation: 3955

Alternatively, add a key CodeCell.cm_config.indentWithTabs with value true in nbconfig/notebook.json (in the jupyter configuration directory).

So, for example the notebook.json should look like this:

{
  "CodeCell": {
    "cm_config": {
      "indentWithTabs": true
    }
  }
}

Upvotes: 2

steve-kasica
steve-kasica

Reputation: 968

If you're trying to swap spaces for tabs in Jupyter's text editor plugin, then you can just switch the "insertSpaces" flag to false in the Advanced Settings Editor.

{
    "editorConfig": {
        "insertSpaces": false
    }
}

Upvotes: 2

Alex
Alex

Reputation: 12913

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

Upvotes: 7

Related Questions