14

Not a long ago I started to learn Mathematica - i.e. I'm novice. Usually I code in text editors with auto close of brackets like Gedit,Notepad++,Qt IDE etc. It's very convenient when you are not obliged to watch over brackets. But my attempts to find similar functionality in Mathematica weren't successful. I can't believe that such a powerful tool doesn't have such simple thing. Does anyone know how to autocomplete brackets?

Tebe
  • 3,176
  • 8
  • 40
  • 60
  • fyi, somewhat related question at SO is http://stackoverflow.com/questions/6137716/mathematica-editor-removing-the-right-matching-automatically-when-the-left – Nasser Dec 13 '11 at 22:43
  • What I do is this: I first type in the parentheses, say (), then after that, I fill the inside. Same for [[ ]]. This might help a little. – Nasser Dec 13 '11 at 22:45
  • yeah, it's good but it's much better when system does this stuff for you and you are able to concentrate at your task – Tebe Dec 13 '11 at 23:19
  • Could this be migrated/included in mathematica.SE? – dearN Mar 31 '12 at 03:09

3 Answers3

18

You could experiment with something like:

SetOptions[InputNotebook[], 
 InputAutoReplacements -> {"[" -> "[\[SelectionPlaceholder]]", 
   "{" -> "{\[SelectionPlaceholder]}", 
   "(" -> "(\[SelectionPlaceholder])"}]

Note that the replacement doesn't happen until you type a character after the opening bracket. If you like the result you can then apply this more globally.

Brett Champion
  • 8,497
  • 1
  • 27
  • 44
  • 1
    If to be honest I expected something like option in menu or plugin. It is not what I was waiting for, but it's even better! – Tebe Dec 13 '11 at 23:30
  • 5
    The menu option is **Insert > Typesetting > Matching []** etc... (with keyboard shortcuts.) But I generally find it easier to just type [], even instead of the shortcut. – Brett Champion Dec 13 '11 at 23:38
  • You might need to modify this so that you can still use the shorthand for `Part`: `[[ ]]` – Simon Dec 14 '11 at 01:15
  • 2
    @Simon It should just work. `x[[` turns into `x[[]` with the insertion point before the last bracket, and then typing a specification (1, All, etc...) will add the second closing bracket. – Brett Champion Dec 14 '11 at 02:19
  • Brett: My bad. Should have checked (or even just thought) before commenting. – Simon Dec 14 '11 at 02:42
  • While this is nice in continuous typing, it is very annoying when you go back and correct/edit your code. For e.g., it works smoothly when write `f[a,g[b]]`, but if I go back to change the `a` to a `c`, then it inserts another `]`, since the cursor was next to `[` to give `f[c],g[b]]`. This compounds the work, since you have to now go back and delete it – abcd Dec 14 '11 at 16:17
  • @yoda And it's also a pain if you want change it to `h[1,f[a,g[b]]]`. I very specifically had the answer only affect a single notebook. Having this enabled by default would personally drive me nuts. – Brett Champion Dec 14 '11 at 16:23
14

As an alternative, Alt+] will produce [] and move the insertion point inside the [].