54
$\begingroup$

Is it possible to program the Front End to automatically format double square brackets without having to type Esc[[Esc each time? It's awful to have to type Esc four times for each Part expression, and even more annoying to visually parse the unformatted double brackets.

See also this entry in MathGroup archive: adding a keyboard shortcut for double brackets

$\endgroup$
3
  • 3
    $\begingroup$ I edited the KeyEventTranslations.tr file to add my own shortcuts. $\endgroup$
    – Silvia
    Commented May 6, 2012 at 2:00
  • $\begingroup$ Thanks Silvia, could you provide details please? $\endgroup$ Commented May 6, 2012 at 2:04
  • $\begingroup$ Assuming you are under win, open the file in a text editor, add a line like Item[KeyEvent["[", Modifiers -> {Control}],FrontEndExecute[{FrontEndNotebookWrite[FrontEndInputNotebook[],"[LeftDoubleBracket]", After]}]] $\endgroup$
    – Silvia
    Commented May 6, 2012 at 2:13

2 Answers 2

53
$\begingroup$

Some approaches are discussed in this question on StackOverflow. Original references to these go to Szabolcs's webpage and a MathGroup posting by Mr.Wizard.

To summarize, you copy the file: $InstallationDirectory/SystemFiles/FrontEnd/TextResources/Macintosh/KeyEventTranslations.tr to $UserBaseDirectory/ (with the same directory tree) and add the following modifications after EventTranslations[{ in the file:

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]
        }]], 

These provide the following shortcuts:

  • using Ctrl+[
  • using Ctrl+]
  • 〚〛 using Ctrl+Cmd+]

Replace Command with Alt for Windows/Linux and modify the paths above accordingly.

You can also try Andrew Moylan's suggestion, in the same post, but I haven't tried it.

$\endgroup$
7
  • $\begingroup$ this is a nice trick, however I couldn't get it to work on a German keyboard. Since the [ and ] keys events are generated by pressing Alt-Gr 8 and Alt-Gr 9 it seems Mathematica does not catch the KeyEvent correctly. Switching over to EN keyboard design works however... Any ideas how to capture Alt-Gr? $\endgroup$
    – Rainer
    Commented Oct 27, 2013 at 10:54
  • $\begingroup$ @xslittlegrass It's not a good idea to modify those files, as they can be overwritten by a new version (if you're in the habit of just having the current version). There might be something else going on with your setup if the above doesn't work. $\endgroup$
    – rm -rf
    Commented May 13, 2014 at 0:38
  • $\begingroup$ On my Windows 7 (64 bit) computer using Mathematica 9.0.0.0, I did not need to replace Command with Alt. The code works as written without modification. $\endgroup$ Commented Aug 12, 2014 at 20:49
  • $\begingroup$ @Rainer It seems Alt-Gr is Command+Alt. I tried to get these commands working with this knowledge with no success. But try typing Shift+Alt+Alt-Gr+9 and you'll get (). These are called "MatchingParentheses" and in the MenuSetup.trlinked with MenuKey[")", Modifiers->{"Command"}. Which would beShift+Alt+9 on a german keyboard. Unfortunately MatchingBraces {} and MatchingBrackets [] dont work. $\endgroup$
    – Phab
    Commented Nov 25, 2014 at 13:44
  • $\begingroup$ @R.M. does this work for you on v11.1? $\endgroup$
    – glS
    Commented Apr 3, 2017 at 11:18
12
$\begingroup$

Edit the KeyEventTranslations.tr file in a plain text editor by adding the following definitions to the first section of the file:

EventTranslations[{

(* Custom shortcuts *)
Item[KeyEvent["[",Modifiers->{Control}],
    FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
    "[\[SelectionPlaceholder]]"],FrontEndToken["MovePreviousPlaceHolder"]}]],

Item[KeyEvent["]",Modifiers->{Control}],
    FrontEndExecute[{FrontEnd`NotebookWrite[FrontEnd`InputNotebook[],
    "\[LeftDoubleBracket]\[SelectionPlaceholder]\[RightDoubleBracket]"],
    FrontEndToken["MovePreviousPlaceHolder"],FrontEndToken["MovePreviousPlaceHolder"]}]],

(* Evaluation *)

Note that these shortcuts are set up to use the "Control" modifier to insert a template for pairs of single/double brackets.

$\endgroup$
1
  • $\begingroup$ These no longer work in 11.2 on windows $\endgroup$
    – Nasser
    Commented Dec 19, 2017 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.