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.