19

Is there a way to avoid having to do ⋮[[⋮ to obtain those great looking brackets for Part?

enter image description here

Is there a way for this to be done automatically after you ran a function or a definition ?

500
  • 6,509
  • 8
  • 46
  • 80
  • 1
    I think [`this`](http://stackoverflow.com/questions/4209405/customizing-mathematica-shortcuts/4209612#4209612) answer could interest you also – Dr. belisarius Sep 07 '11 at 02:03
  • This SO discussion appears relevant to your question. (http://stackoverflow.com/questions/5461688/how-do-i-get-auto-conversion-of-part-double-brackets-on-paste) – DavidC Sep 07 '11 at 07:25

2 Answers2

17

I have the following addition in /Applications/Mathematica.app/SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr which lets me enter double brackets with key combinations. You can do the same by modifying the file (where ever it is on your OS). I first learnt of this from Szabolcs's website here. He has other mathematica related stuff there that might be of help to you.

The commands added are:

  • with Ctrl+[
  • with Ctrl+]
  • 〚〛 with Ctrl+Alt+]

Equivalents, as listed in the KeyEventTranslations.tr file are:

Modifiers can be "Shift", "Control", "Command", "Option"

For Macintosh: "Command" = Command Key, "Option" = Option Key

For X11: "Command" = Mod1, "Option" = Mod2

For Windows: "Command" = Alt, "Option" = Alt

Insert the following after EventTranslations[{ in the above file.

(* Custom keyboard shortcuts *)
    Item[KeyEvent["[", Modifiers -> {Control}],
        FrontEndExecute[{
            FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
                "\[LeftDoubleBracket]", After]
        }]],
    Item[KeyEvent["]", Modifiers -> {Control}],
        FrontEndExecute[{
            FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
                "\[RightDoubleBracket]", After]
        }]], 
    Item[KeyEvent["]", Modifiers -> {Control, Command}],
        FrontEndExecute[{
            FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
                "\[LeftDoubleBracket]", After],
            FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
                "\[RightDoubleBracket]", Before]
        }]], 

You're not the only one who's peeved by it. Here's my attempt to avoid having to stretch to hit Esc by mapping Caps lock to Esc. Mr. Wizard also had a couple of questions related to conversion of [[ to .

Community
  • 1
  • 1
abcd
  • 41,765
  • 7
  • 81
  • 98
  • So I need to save the Item[] code at the above location ? Looks awesome but I can`t implement it :( – 500 Sep 07 '11 at 00:02
  • 2
    @500 I had forgotten to include where to insert it. Please see my edit. Also, **backup** the file. – abcd Sep 07 '11 at 00:11
  • Edit is great, thank you for the site it is rich ! I already decided to do this in the morning ;-) – 500 Sep 07 '11 at 00:32
  • yoda, in my file I have a KeyEvent for `[[]]` -- may I add it to your answer? – Mr.Wizard Oct 24 '11 at 20:30
  • @Mr.Wizard Sure, you may add it. I've seen your solution on the Mathgroup site, but forgot about it when I wrote the answer. – abcd Oct 24 '11 at 20:32
  • I noticed that you have literally `"[["` and `"]]"` in your code above, rather than `"\[LeftDoubleBracket]"` etc. Are these auto-converted? – Mr.Wizard Oct 24 '11 at 20:36
  • @Mr.Wizard You are indeed right that it should be `\[LeftDoubleBracket]`. What happened was that when I wrote the answer, I quickly opened `KeyEventTranslations.tr` in mma since I had it running. I shouldn't have, because it's supposed to be a text file and Mma automatically converts any `\[LeftDoubleBracket]` it sees into `〚`, which upon copy-paste into this answer, got converted into `[[`. I hadn't even noticed this typo, and it didn't affect my shortcut because the actual file wasn't modified (I had merely opened) still contains `\[Left...`. Fixed the typo now :) – abcd Oct 24 '11 at 20:59
10

My preference is the following (code fixed thanks to Sjoerd C. de Vries):

n = SelectedNotebook[];
SetOptions[n, 
 InputAliases -> 
  Append[Options[n, InputAliases][[1, 2]], 
   "[]" -> "\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]\[Placeholder]"]]

This adds a new input alias ⋮[]⋮ that inserts both [[ and ]], places the cursor on a placeholder inside the brackets, and puts another placeholder outside the brackets which you reach by pressing Tab.

Try it and see. If you like it, you can add it to your Global options: Format -> Option Inspector -> Show option values -> Global preferences -> Search for InputAliases.

You could also combine this with the keyboard shortcut solution proposed by yoda.

Andrew Moylan
  • 2,893
  • 18
  • 20
  • 2
    It doesn't work as you wrote given the extra layer of {} in the output of `Options`. Something like `SetOptions[n, InputAliases -> Append[Options[n, InputAliases][[1, 2]], "[]" -> "\[LeftDoubleBracket]\[SelectionPlaceholder]\ \[RightDoubleBracket]\[Placeholder]"]]` works for me – Sjoerd C. de Vries Sep 07 '11 at 12:38
  • @SjoerdC.deVries Yours works for me. The _only_ reason you'd enter `[[` is if you were using `Part`... so is it possible to map `[[` (without hitting Esc) to automatically insert the nice brackets and placeholders? Something like how `->` gets auto-converted into an arrow. – abcd Sep 07 '11 at 14:26
  • @yoda The problem is that for the right brackets, `Part` is not the only thing that may produce `]]`, and not even the most frequent one. But having such a one-sided shortcut does not strike me as an aesthetic solution. – Leonid Shifrin Sep 07 '11 at 16:18
  • @LeonidShifrin That's correct, which is why I asked if merely typing `[[` (which is only for `Part`) could be made to automatically result in placing the _entire thing_ `〚◼〛◻` as in Andrew's solution. Nothing should happen if you were to type `]]`. – abcd Sep 07 '11 at 16:27
  • @yoda Oh, I see. Sounds like a good suggestion, particularly if it would be easy to switch on and off. – Leonid Shifrin Sep 07 '11 at 16:43
  • @SjoerdC.deVries Thanks, you're right. My attempt to test my code failed because I already had it set as a global option! – Andrew Moylan Sep 07 '11 at 18:10