# How can I automatically replace [[ and ]] with the \[LeftDoubleBracket] and \[RightDoubleBracket] operators?

When you type ->, it is automatically turned into the \[Rule] character. Can we configure our frontends to do the same for the [[ and ]] characters, which could be turned into \[LeftDoubleBracket] and \[RightDoubleBracket]?

You can easily do this manually by typing <esc>[[<esc>, but I would like to do this automatically.

This question was originally asked by me in the chat and has answers there; this question is so that those answers can be added and kept for the community.

• Do you really want that when writing func[func2[arg]]? I changed KeyEventTranslations.tr to have <kbd>Alt gr</kbd>+<kbd>i</kbd>/<kbd>o</kbd>. – Coolwater Apr 7 '19 at 19:04
• Yes, this is part of the problem. Ideally it would only happen after you have typed the matching [[, but more than likely it needs to be done at shift-enter at the earliest. For [[ the problem is simpler because there is no time when you would type [[ except for as part of a Part expression. – Carl Lange Apr 7 '19 at 19:05
• closely related: 98175 – Kuba Apr 7 '19 at 20:50
• Related using KeyEventTranslations 5221 – Rohit Namjoshi Apr 7 '19 at 21:41
• Unless @Kuba or another mod overrules, I will leave this open as I am asking how to do it as-I-work rather than afterwards, but the questions are certainly strongly related. – Carl Lange Apr 7 '19 at 21:42

You can use CellEpilog to modify the contents of the cell upon evaluation:

SetOptions[EvaluationNotebook[],
CellEpilog :> If[
! TrueQ[
Max@CurrentValue[CellChangeTimes] <=
CurrentValue[EvaluationCell[], {TaggingRules, "autoreplaceTime"}]
],
CurrentValue[EvaluationCell[], {TaggingRules, "autoreplaceTime"}] =
Max@CurrentValue[CellChangeTimes];
NotebookWrite[
EvaluationCell[],
RowBox@{head_, "[", RowBox@{"[", part_, "]"}, "]"} :>
]
]
]


Demonstration:

This could of course be extended to do similar replacements for <|…|> etc. To minimize the performance hit, we keep track of the last CellChangeTimes value and only rerun the replacement code when it changed.

The nice thing about an approach like this (i.e. manipulation of the cell's box structure) is that it's rather straightforward to identify the bracket pairs to replace, since the front-end does all the necessary parsing for us.

An alternative to putting the code into CellEpilog might be to create a menu item for it, one could also modify the "Evaluate cells" menu item to run this code on all selected cells first.

Here is a stylesheet solution modeled after Lucas' answer, except that I use CellEventActions to tie the transformation to a keyboard short cut:

SetOptions[
EvaluationNotebook[],
StyleDefinitions -> Notebook[{
Cell[StyleData[StyleDefinitions->"Default.nb"]],
Cell[StyleData["Input"],
CellEventActions -> {
{"KeyDown","y"} :> If[CurrentValue["ControlKey"],
NotebookWrite[
EvaluationCell[],
Cell[
ReplaceAll[
RuleDelayed[
]
],
]
]
];
SelectionMove[EvaluationNotebook[],Before,CellContents];
SelectionMove[EvaluationNotebook[],After,CellContents]
],
PassEventsDown->True
}]
},
StyleDefinitions->"PrivateStylesheetFormatting.nb"
]
]


Using the short cut ctrl + y will convert [[ and ]] to the double bracketing bar versions.

It's maybe not exactly what you want but you can always select the cell, expression, or part of it ( Ctrl-. is helpful for this ) and then do Ctrl-Shift-n to change the selection to Standard Form. This includes the changes to [[ and ]] and doesn't require any set up or customisation.