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

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[],
    Replace[NotebookRead[EvaluationCell[]],
     RowBox@{head_, "[", RowBox@{"[", part_, "]"}, "]"} :>
      RowBox@{head, "\[LeftDoubleBracket]", part, "\[RightDoubleBracket]"}, All]
    ]
   ]
 ]

Demonstration: https://i.stack.imgur.com/4V5WJ.gif

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[],
                        With[{read=NotebookRead[EvaluationCell[]]},
                            Cell[
                                ReplaceAll[
                                    First[read],
                                    RuleDelayed[
                                        RowBox[{head_,"[",RowBox[{"[",part_,"]"}],"]"}],
                                        RowBox[{head,"\[LeftDoubleBracket]",part,"\[RightDoubleBracket]"}]
                                    ]
                                ],
                                Sequence@@Rest[read]
                            ]
                        ]
                    ];
                    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.