Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I like to use terminals (e.g. gnome-terminal) where you can use the up key to copy the last command into the current input 'field'. For example, to repeat the last command you just have to press up, enter. Or, to modify the last input and re-run, it is just up, modify, enter.

As beginner I often make mistakes, or experiment by slightly changing previous inputs. Is there any way in which I can obtain this convenient input behavior in the Mathematica front end?

share|improve this question
2  
You can always just go back and edit that cell. Notebooks are not linear like a traditional command line. You can edit any previous input without needing to "recall" it. Perhaps you'll find this way of working more efficient if you get a bit used to it. –  Szabolcs Mar 7 '13 at 17:25
    
Have you seen edit to my answer? I am interested if it works with somebody else, because I didn't test it much. –  Vladimir Apr 19 '13 at 23:12

2 Answers 2

One easy thing to do is to get used to naming your cells. Start your cell with

 x1 = (cell contents)

then you can later go back and it already has a name. Alternatively, you can refer to cells by their number;

 In[27]

is the 27th cell defined in this session.

share|improve this answer
    
If I write 5! in cell 10, then In[10] evaluates to 120. Ideally, I need to be able to press something (e.g.<up>) during input mode in cell 11 that makes content of In[11] sames as In[10] (while still possible to edit it) –  ipy Mar 7 '13 at 17:13
    
There is also Control-L (Apple-L) that will copy down the input cell above your insertion point. –  Seth Chandler Mar 7 '13 at 17:18
    
Thank so much for this! (any idea if something works for depth more than 1?) –  ipy Mar 7 '13 at 17:42

Have you tried shortcuts like cmd+L and cmd+Shift+L ? Perhaps these can be even overridden and generalized for next to last Input/Output cells and above. But, I'm still not sure how to do it.

Edit

Ok, so I managed to do it. (It took me almost two days, but I was inspired..)

To modify keyboard shortcuts or add new, it's necessary to change KeyEventTranslations.tr and/or MenuSetup.tr located in

$InstallationDirectory/SystemFiles/FrontEnd/TextResources/$OperatingSystem/

There are already several good ideas for making user defined keyboard shortcuts or menu items (1, 2, 3, 4, 5...), which can really save you some time at work once you adjust them as it suits. As these are system files, make sure to make a back-up before editing them. When/If you get more familiar with adding new shortcuts, you may also consider automating this procedure as shown here by Rolf Mertig.

For listing previous inputs in the selected notebook, in their original input form (what you asked), I made two new shortcuts and pasted them using text editor in KeyEventTranslations.tr right after the line EventTranslations[{

(*
Command Up   -   creates Terminal cell in which user can list previous inputs upwards to oldest
*)
Item[KeyEvent["Up", Modifiers -> {Command}], 
KernelExecute[With[{nb = SelectedNotebook[]},
 If[And[ValueQ[$TraceLine], NotebookFind[nb, "Terminal", Next, CellLabel] =!= $Failed],
  If[$TraceLine > 1,
	   $TraceLine = $TraceLine - 1;
	   SelectionMove[nb, All, CellContents];
	   NotebookWrite[nb,
	    ReplaceAll[ToBoxes[ReplaceAll[ReplaceAll[Hold[In[n] // InputForm], n -> $TraceLine], DownValues[In]]], RowBox[{"Hold", "[", Longest[Pattern[x,BlankSequence[]]], "]"}] -> x]
   ]
   , Beep[]
  ]
  , If[$Line > 1,
	   $TraceLine = $Line - 1;
	   SelectionMove[nb, After, CellGroup, 2];
	   NotebookWrite[nb, Cell[
	    BoxData[ReplaceAll[ToBoxes[ReplaceAll[ReplaceAll[Hold[In[n] // InputForm], n -> $TraceLine], DownValues[In]]], RowBox[{"Hold", "[", Longest[Pattern[x,BlankSequence[]]], "]"}] -> x]],
    "Input", CellLabel -> "Terminal", CellLabelAutoDelete -> False, ShowCursorTracker -> False
   ]];
   FrontEndExecute[FrontEndToken["MovePrevious"]]
   , Beep[]
  ]
 ]
]], MenuEvaluator -> Automatic
],
(*
Command Down   -   lists down previous inputs in Terminal cell down do most recent
*)
Item[KeyEvent["Down", Modifiers -> {Command}], 
KernelExecute[With[{nb = SelectedNotebook[]},
 If[And[ValueQ[$TraceLine], NotebookFind[nb, "Terminal", Next, CellLabel] =!= $Failed, $TraceLine < $Line - 1],
  $TraceLine = $TraceLine + 1;
  SelectionMove[nb, All, CellContents];
  NotebookWrite[nb,
   ReplaceAll[ToBoxes[ReplaceAll[ReplaceAll[Hold[In[n] // InputForm], n -> $TraceLine], DownValues[In]]], RowBox[{"Hold", "[", Longest[Pattern[x,BlankSequence[]]], "]"}] -> x]
  ]
  , Beep[]
 ]
]], MenuEvaluator -> Automatic
],

Abilities:

  • I've chosen ⌘↑ and ⌘↓, but one can as well set different in element KeyEvent[], where Modifiers can be "Shift", "Control", "Command", "Option"
    • For Macintosh: "Command" = Command Key, "Option" = Option Key
    • For Windows: "Command" = Alt, "Option" = Alt
  • When choosing key shortcut, be sure that there are no overlaps afterwards. For example, I have deleted original shortcuts for ⌘↑ and ⌘↓, from the file KeyEventTranslations.tr, before using them! But you can find some other or override original ⌘L

  • This routine uses global variable $Line and function In[]. Therefore functionality depends on setting $HistoryLength (through In[]). The routine also creates one global variable $TraceLine, which is unprotected as well as $Line

  • When pressing first time the shortcut key it creates a new cell close to current position of the cursor with a label "Terminal", where previous inputs are being listed (with a possibility for the change). After evaluation of the cell it will change its label to "In[$Line]:=" as all other input cells.
  • Before evaluating the cell "Terminal" user can go around and change, evaluate other cells and then continue listing inputs where he/she stopped, but only at the same "Terminal" cell - more than one will not be created, instead it will move your cursor automatically to that cell.

Any suggestions for improvements or simplifications are welcome.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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