Skip to main content
Added a note on the downside of this answer
Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Presuming you only want to this special output to come from computations that bind a variable to the value of the computation, here is one way it can be done by $Pre and $Post:

SetAttributes[saveSet, HoldAll];
saveSet[form : Set[var_, _]] := (lastSet = ToString@Unevaluated@var; form);
saveSet[form : ___] := (lastSet =.; form)

$Pre = saveSet;

$Post = (If[ValueQ@lastSet, Row[{lastSet, " = ", #}], #]) &;

After these definitions are evaluated, computations using Set (=) show up as:

y = 42^2 + 1

y = 1765

but other expression evaluations will printout normally:

x == y

False

The downside of this is that, when these definitions are in effect, % becomes unusable after a Set evaluation. Condsider

y = 42^2 + 1

y = 1765

% - 1

-1+y = 1765

Presuming you only want to this special output to come from computations that bind a variable to the value of the computation, here is one way it can be done by $Pre and $Post:

SetAttributes[saveSet, HoldAll];
saveSet[form : Set[var_, _]] := (lastSet = ToString@Unevaluated@var; form);
saveSet[form : ___] := (lastSet =.; form)

$Pre = saveSet;

$Post = (If[ValueQ@lastSet, Row[{lastSet, " = ", #}], #]) &;

After these definitions are evaluated, computations using Set (=) show up as:

y = 42^2 + 1

y = 1765

but other expression evaluations will printout normally:

x == y

False

Presuming you only want to this special output to come from computations that bind a variable to the value of the computation, here is one way it can be done by $Pre and $Post:

SetAttributes[saveSet, HoldAll];
saveSet[form : Set[var_, _]] := (lastSet = ToString@Unevaluated@var; form);
saveSet[form : ___] := (lastSet =.; form)

$Pre = saveSet;

$Post = (If[ValueQ@lastSet, Row[{lastSet, " = ", #}], #]) &;

After these definitions are evaluated, computations using Set (=) show up as:

y = 42^2 + 1

y = 1765

but other expression evaluations will printout normally:

x == y

False

The downside of this is that, when these definitions are in effect, % becomes unusable after a Set evaluation. Condsider

y = 42^2 + 1

y = 1765

% - 1

-1+y = 1765

Source Link
m_goldberg
  • 108.1k
  • 16
  • 104
  • 259

Presuming you only want to this special output to come from computations that bind a variable to the value of the computation, here is one way it can be done by $Pre and $Post:

SetAttributes[saveSet, HoldAll];
saveSet[form : Set[var_, _]] := (lastSet = ToString@Unevaluated@var; form);
saveSet[form : ___] := (lastSet =.; form)

$Pre = saveSet;

$Post = (If[ValueQ@lastSet, Row[{lastSet, " = ", #}], #]) &;

After these definitions are evaluated, computations using Set (=) show up as:

y = 42^2 + 1

y = 1765

but other expression evaluations will printout normally:

x == y

False