84

I stay up-to-date with ipython's dev branch (because ipython is pretty much the most awesome thing ever). Fairly recently (before yesterday's awesome ipython 2.0 release) I noticed that it has started to automatically close parentheses, brackets, quotes, etc., as I type them. It happens in both terminal [nothing else I use in terminal does it] and notebook sessions, so I assume it was an intentional choice on the part of the developers. I can respect that other people might like this feature, but it drives me completely nuts.

I can't find any option for it in the configuration files. I can't even google for it, because I don't know what it's called. The only thing that comes up is the different feature of automatic parentheses. I did actually find this question, but that's old, and suggests that the behavior I'm seeing can't happen.

How can I turn this feature off?

[I mostly just use the notebook interface anyway, so just turning it off there would be fine, but I'd prefer to turn it off in both notebooks and ipython sessions at the terminal.]

Mike
  • 19,114
  • 12
  • 59
  • 91

9 Answers9

32

@minrk's answer is the meat and bones of the fix, but you'll need to wrap it in an initialization callback, at least with IPython-3.1.0. In your custom.js:

require(['base/js/namespace', 'base/js/events'], function(IPython, events) {
  events.on('app_initialized.NotebookApp', function() {
    IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
  });
});

Thanks @Mike for your comment about IPython's RequireJS dependency loading and the pointer to a better formulation at IPython/Jupyter Installing Extensions.

Edit for Jupyter 4.0.x:

The current IPython notebook implementation, Jupyter 4.0.0, revamped JS customizations. It now uses ~/.jupyter/custom/custom.js by default, and you'll need to replace that whole require(... events.on(...)) snippet with just the following in global scope:

IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;

Likewise, if you want to use jQuery to manipulate anything, just use the jQuery global directly. For example, I like to hide the fixed header by default, which gives me another 40px of space for my code, which I find a bit more valuable than looking at the Jupyter logo all the time:

jQuery('#header-container').hide();

Edit for Jupyter ≥ 4.0.6 (but < Jupyter Lab):

If the custom.js solution above doesn't work, try adding the following to your ~/.jupyter/nbconfig/notebook.json:

{
  "CodeCell": {
    "cm_config": {
      "autoCloseBrackets": false
    }
  }
}
chbrown
  • 11,865
  • 2
  • 52
  • 60
  • 1
    Note that `$` is only defined about 2/3 the time nowadays, because the startup process has changed. As I pointed out in [this answer](http://stackoverflow.com/a/29451242/1194883), and as you can see from the `custom.js` that ipython creates nowadays, you need to use `require`. – Mike Apr 17 '15 at 15:09
  • 3
    In Ipython/Jupyter 4.0 this does not work for me anymore. Anybody ran into the same problem? – bjonen Sep 09 '15 at 19:45
  • Hiding the #header-container also hides the name of the file (no big deal, just use File > Rename to see/edit the name) and the little message that says when it was autosaved (I guess it's not that important...). – MD004 Feb 03 '16 at 05:04
  • 1
    Your instructions for Jupyter 4.0 seem to work for Jupyter 5.0 also, I'm glad to see. – nealmcb Apr 11 '17 at 01:35
  • I didn't get the version for 4.0 to work on 6.0.3, but there is an updated answer here which worked for me: https://stackoverflow.com/questions/44216326/how-to-disable-auto-quotes-and-auto-brackets-in-jupyter-5-0 – doublefelix Oct 16 '20 at 15:27
  • Or you can run `from notebook.services.config import ConfigManager ; c = ConfigManager() ; c.update('notebook', {"CodeCell": {"cm_config": {"autoCloseBrackets": False}}}) ` in Jupyter 4.6.X , especially if that config file already exists – T. Zack Crawford Jan 13 '22 at 05:36
24

The notebook behavior is the result of the CodeMirror autoCloseBrackets plugin. You can turn this off by editing (create it with ipython profile create if you haven't already) ~/.ipython/profile_default/static/custom/custom.js and adding:

if (IPython.CodeCell) {
  IPython.CodeCell.options_default.cm_config.autoCloseBrackets = false;
}

As for the terminal, I don't see the parenthesis behavior you describe. Do you perhaps have a PYTHONSTARTUP defined? IPython executes this file by default, which you can disable by adding to ~/.ipython/profile_default/ipython_config.py:

c.InteractiveShellApp.exec_PYTHONSTARTUP = False
minrk
  • 37,545
  • 9
  • 92
  • 87
  • Would it even work in the terminal? Does readline support typing `)` over an existing `)`? – asmeurer Jun 01 '14 at 18:39
  • 2
    The command `$ ipython profile create` will say that it created a `.py` file. Do not edit that file. Instead edit the file `~/.ipython/profile_default/static/custom/custom.js`, like the answer says. How can anyone type code with that annoying auto closing parentheses? – 7stud Jan 10 '15 at 08:54
  • 5
    This appears not to work in IPython 3.1.0/Jupyter (I tried deleting my `~/.ipython` and starting from scratch). Any idea how to do it in newer versions? Also, how about destroying the even more annoying quote auto-closing behaviour? – naught101 Apr 15 '15 at 03:53
  • [chbrown's answer above](http://stackoverflow.com/a/29702505/1194883) gives the new way to do this. – Mike Jul 14 '15 at 13:40
  • 1
    Also tried chbrown's method in IPython 4.0/Jupyter with no luck; still getting my quotes auto-closed. Any update to this? – horatio1701d Aug 18 '15 at 14:51
14

If you want to do it just from python:

from notebook.services.config import ConfigManager
c = ConfigManager()
c.update('notebook', {"CodeCell": {"cm_config": {"autoCloseBrackets": False}}})
Sean D
  • 381
  • 3
  • 14
11

This is what works for me in Jupyter 4.0.6:

require(['notebook/js/codecell'], function (codecell) {
  codecell.CodeCell.options_default.cm_config.autoCloseBrackets = false;
})

in ~/.jupyter/custom/custom.js.

BTW, If you additionally want to switch off the syntax higlighting of matching parentheses:

codecell.CodeCell.options_default.cm_config.matchBrackets = false;
kkumer
  • 331
  • 3
  • 3
7

In the JupyterLab Notebook you can turn off the autoClosingBrackets plugin in the settings menu. Go to Settings --> Advanced Settings Editor and add the following in the User Overrides section:

{
  "codeCellConfig": {
    "autoClosingBrackets": false
  }
}

Screenshot image

This worked with JupyterLab 0.32.1 and jupyter_core 4.4.0

joelostblom
  • 43,590
  • 17
  • 150
  • 159
5

The above suggestions didn't worked for me in Jupyter 4.3.0 with Jupyter Notebook 5.0.0

I found that I needed to create a file called ~/.jupyter/custom/custom.js with the following contents:

var cell = Jupyter.notebook.get_selected_cell();
var patch = {
  CodeCell: {
    cm_config: {
      autoCloseBrackets: false,
    }
  }
}
cell.config.update(patch);

Note that the directory ~/.juypter/custom didn't exist before I did this.

This was hacked together from suggestions in v5.0 docs, and for future readers these are the latest

Sam Mason
  • 15,216
  • 1
  • 41
  • 60
4

For Jupyter Notebook 5.1 use the same thing as for 4.2, i.e. put the following snippet into ~/.jupyter/custom/custom.js:

require(['notebook/js/codecell'], function (codecell) {
  codecell.CodeCell.options_default.cm_config.autoCloseBrackets = false;
})
Sergey
  • 2,906
  • 3
  • 27
  • 32
  • This worked for me on 5.0.0 when Sam's solution did nothing. Oh dear. – Heath Raftery Aug 23 '17 at 02:59
  • 1
    Neither of these work for me anymore, but this solution does: https://stackoverflow.com/questions/44216326/how-to-disable-auto-quotes-and-auto-brackets-in-jupyter-5-0 – Jim Garrison Oct 11 '18 at 00:21
3

I found it was not mentioned in other answers. In my case(OS X, Jupyter 4.2.0), custom.js is located in ~/anaconda/lib/python3.5/site-packages/notebook/static/custom/custom.js

I think it may help somebody like me.

wuliang8910
  • 271
  • 3
  • 4
2

We can do that from jupyter console, try it.

enter image description here

muntasir kabir
  • 168
  • 2
  • 13