Sometimes it may be useful to use (%%…%) (k times) to get the kth previous result in a notebook instead of setting a variable.

However, if I want to get the 8th previous result, for example, I would have to type (%) eight times (%%%%%%%%) and I could get lost. Needless to remember that every time (%) is needed (Shift)(5) must be pressed, which demands both hands and is somewhat inconvenient.

So, in the code below, I have defined a new command.

Now, when I want the previous 4th result I just type (o4) instead of (%%%%). If I wanted a previous result before the 9th, say the 14th, I would have to change the Range[9] in the command to Range[14] and generate (o1…o14). I could also obtain only the previous 3rd to the 7th values using Range[3, 7], getting (o3…o7).

This command should be run every time a new session of Mathematica is initiated. It could be written in an initialization cell in the notebook. Remember that you should not redefine or clear (o1…o9) at the cost of getting unexpected errors. You must consider them as "system variables".

(* Define o1,o2,...,o9 as "%","%%",...,"%%%%%%%%%" *)
Map[ToExpression[StringJoin["o", ToString[#], " := ", "Out[$Line - ", ToString[#], "]"]] &, Range[9]];


{Sin[%], Sin[o1]}
{Cos[%%], Cos[o2]}
{Tan[%%%], Tan[o3]}
{Sin[2 %%%%], Sin[2 o4]}
{Cos[2 %%%%%], Cos[2 o5]}
{Tan[2 %%%%%%], Tan[2 o6]}

{0.5, 0.5}
{0.866025, 0.866025}
{0.57735, 0.57735}
{0.866025, 0.866025}
{0.5, 0.5}
{1.73205, 1.73205}
  • 6
    $\begingroup$ Just use Out. See the "Details" section. $\endgroup$ – Alan Sep 25 '19 at 19:15
  • 3
    $\begingroup$ As in Out[-5] (i.e., five lines ago). Though frankly, in such a situation, I would start with foo = Pi/6. and then use foo instead of % or Out. $\endgroup$ – Michael E2 Sep 25 '19 at 19:19
  • $\begingroup$ Maybe try With[ { foo = Pi/6 }, { {Sin[ foo ], Sin[ o1 ]}, ... } ] and use Column to arrange the output. $\endgroup$ – LouisB Sep 25 '19 at 19:31
  • $\begingroup$ Out[-k] is not as convenient as the command presented. What I wanted to show is a simpler form of getting previous values writing only o1, o2 &c, with only 2 keystrokes instead of having to use 8 keystrokes every time I use Out[-k] or (k +1) keystrokes when I use (%). $\endgroup$ – Caio Sep 25 '19 at 21:19

% is just a shorthand for Out[-1] or even Out[]. And so on, Out[-k] gives the k-th previous result. Or from the details section of Out

Out[-k] is equivalent to %%...% (k times).

One way I've used Out, along with the way you can get multiple outputs by giving it a list of negative integers, before is testing timing and equality between different methods.

Range[-10^6, 10^6]~Complement~{0} // AbsoluteTiming;
Join @@ {-Reverse@#, #} &@Range[10^6] // AbsoluteTiming;
Out[{-1, -2}][[;; , 1]]
SameQ @@ Out[{-1, -2} - 1][[;; , 2]]

{0.0201598, 0.0370587}


Or maybe a more clear example

Out[{-1, -2}]

{b, a}


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