This question has been asked before on stack overflow. However we will summerize some of the answers given there on our new Mathematica site.
Wrap In Compound Expression
One suggestion by Michael Pilat, given there was to wrap your lines in CompoundExpression
, e.g.
(
Print@Range[5];
Abort[];
Print@Range[5];
)
During evaluation of In[39]:= {1,2,3,4,5}
$Aborted
If you mind the formatting of parenthesis, you could explicitly use the FullForm
of CompoundExpression
like this:
CompoundExpression[
Print@Range[5],
Abort[],
Print@Range[5],
]
Use of $PreRead
Suggested by Alexey Popkov:
In[1]:= $new$PreRead = False;
AbortAllPendingInputs :=
AbortProtect[If[! $new$PreRead, $new$PreRead = True;
$TimeOfAbort = SessionTime[];
last$PreRead = ToString[Definition[$PreRead], InputForm];
ClearAll[$PreRead];
$PreRead := If[TrueQ[SessionTime[] - $TimeOfAbort < 1], "",
$new$PreRead = False;
ClearAll[$PreRead];
If[last$PreRead === "Null", #,
ToExpression[last$PreRead]; $PreRead@#]
] &;]];
In[3]:= f := CheckAbort[Pause[10], AbortAllPendingInputs; Abort[]]
In[4]:= While[True, f]
While[True, f]
While[True, f]
Out[4]= $Aborted
Note that this solution will need to be modified if you use $PreRead
for anything else.
Use of CellEvaluationFunction
CellEvaluationFunction
gets the BoxData
expression, before it is even split. Just assigning something like CellEvaluationFunction:>ToExpression
seems to do the trick.
You can set it as an Input cell style, or as a notebook option, or front end session option, or however you like
SetOptions[$FrontEndSession, CellEvaluationFunction :> ToExpression]
However, you lose the multiple outputs in those new lines when you don't use ;. This can probably be solved but I don't know very well how CellEvalutionFunction works, and how can it receive a box structure, default to Identity but expect to return an expression. Edit should you come up with any improvements