Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there a command / menu entry / keyboard shortcut to evaluate all cells above / up to the current cursor position? I have the feeling I am missing something elementary here...

Addendum: In fact, it is more about selection of those cells, evaluation is not the problem as such.

share|improve this question
Shift-Home Shift-Enter maybe? (on a keyboard that has Home) – R. M. Dec 20 '12 at 14:38
EXTREMELY useful! I've been suggesting this feature to WRI for several years. – Andreas Lauschke Dec 21 '12 at 20:08
@AndreasLauschke phew, glad I´m not the only one. And it seems such a minor effort to add this to the menu/shortcuts. Whom at WRI should we bug to reinforce your effort? – Yves Klett Dec 21 '12 at 20:16
Useless. Forgeddaboudit. But you could put Rolf's or Martin's or Rojo's solution in an init.m, to put it on a button in a palette, or use the keytranslate. I have no use for the Caps Lock key, I find it the most useless key ever, so I mull putting this on Caps Lock. Solves two problems at once, and turns them into solutions. I don't like overloading Shift and Ctrl keys too much, as I have other key translations for them on an o/s level, but Caps Lock is free and otherwise useless. – Andreas Lauschke Dec 21 '12 at 20:25
@AndreasLauschke I like the Caps Lock two-in-one fix. No more unneccessary sHOUTING as well! Hmm, a productive evening. – Yves Klett Dec 21 '12 at 20:30
up vote 19 down vote accepted

One possibility is to modify your personal (only tested on Windows). Evaluate the following, then restart Mathematica, then Ctrl+Shift+Home will select all cells above the insertion point.

For 9.0.1. use: Import[""]

         (* Select all cells upwards *)
         Item[KeyEvent[\"Home\", Modifiers -> {Control, Shift}], 
          Module[{ enb = EvaluationNotebook[],
                   tag = StringJoin[\"tmp\", ToString[Round[AbsoluteTime[]/$TimeUnit]]],editable 
                    editable = ReplaceAll[Editable, Options[enb, Editable]];
                    SetOptions[enb, Editable -> False];
                    SelectionMove[enb, Previous, Cell, AutoScroll -> False];
                    MathLink`CallFrontEnd[FrontEnd`SelectionAddCellTags[enb, {tag}]];
                    SelectionMove[enb, Before, Notebook, AutoScroll -> False];
                    SelectionMove[enb, Next, Cell, AutoScroll -> False];
                    While[FreeQ[ReplaceAll[CellTags,Options[NotebookSelection[]]], tag],
                          MathLink`CallFrontEnd[FrontEnd`SelectionAddCellTags[enb, {tag}]];
                          SelectionMove[enb, Next, Cell, AutoScroll -> False]
                    NotebookFind[enb, tag, All, CellTags, AutoScroll -> False];
                    MathLink`CallFrontEnd[FrontEnd`SelectionRemoveCellTags[enb, {tag}]];
                    SetOptions[enb, Editable -> editable]
                ], MenuEvaluator -> Automatic ]
With[{os = Switch[$OperatingSystem,"MacOSX","Macintosh","Windows","Windows","Unix","X"]},
mykeytext=StringReplace[keytext,"EventTranslations[{":>StringJoin["EventTranslations[{\n(* User defined *)\n",mymenuitems,",\n"]];
share|improve this answer
(...drawing a deep breath...) AWESOME! This is pure gold, and with bells and whistles as well. My workflow sends most sincere regards and goes home early to celebrate. – Yves Klett Dec 21 '12 at 18:43
@YvesKlett Gern geschehen. I actually added SelectionEvaluate[SelectedNotebook[]]; at the end of KernelExecute, since I usually like to do that. It is a matter of personal working style I guess if one wants this or not. Maybe people at WRI work differently? Or have smaller notebooks? Or don't use notebooks really for testing (being WWB freaks all of them?). Who knows. At least it is sufficiently easy to modify/improve things in Mathematica. – Rolf Mertig Dec 21 '12 at 19:00
slightly modified to work also under Linux correctly ... Notice that I personally find Control Shift UpKey easier. So what I use can be used like Import[""] – Rolf Mertig Dec 22 '12 at 22:22
Updated the comments for 9.0.1. Use: Import[""] – Rolf Mertig Feb 18 '13 at 16:57
Very, very nice. The given code can be (very) slightly modified to create a shortcut that selects all cells below the current cell, using Ctrl+Shift+DownArrow. After the definition of mymenuitems, just add the following line: mymenuitems=StringReplace[mymenuitems, {"Home" -> "Down","Next" -> "Previous", "Previous" -> "Next", "Before" -> "After"}];. – Eric Thewalt Mar 13 '13 at 8:39

I was just working on something and here's a side effect. Nothing new but given in closed form as palette button:

 Button["Evaluate above",
  With[{NB = InputNotebook[]},
   Do[ SelectionMove[Experimental`FromCellIndex[NB, i], All, Cell];
       , {i, 1, Experimental`ToCellIndex @ SelectedCells[NB][[1]]}]

Cursor must not be between cells but in the cell that is meant to be evaluated at the end.

share|improve this answer

You can evaluate the input cells before the current cell by running this

   SelectionMove[cobj, Cell, All];
  TakeWhile[Cells[CellStyle -> "Input"], # =!= EvaluationCell[] &];
SelectionMove[EvaluationCell[], After, Cell];
share|improve this answer
Hooray! Only thing missing now is a convenient shortcut (but this is also already covered by @RolfMertig´s answer). – Yves Klett Dec 21 '12 at 19:39
shortcut or button in a palette. I'll probably put this on a button on a palette, together with other useful buttons. – Andreas Lauschke Dec 21 '12 at 20:10
I don't think this is so easy to put into a button, since you need EvaluationCell[] – Rolf Mertig Dec 21 '12 at 23:47
@RolfMertig in that case, ExperimentalPreviousCell[]` makes more sense – Rojo Dec 22 '12 at 0:08

I have no idea how to do this with relation to the cursor in general, but using CellObject you can do a lot if you're in a cell.

There's quite a bit you can do with the SelectionMove function. The following will select all the cells in the evaluation notebook. Note that quite strangely All is specifying the current notebook, After and Previous will give you the respective element of the list of notebooks provided by evaluating Notebooks[].

SelectionMove[EvaluationNotebook[], All, Notebook]

The selection can be moved according to CellGroup. This button will progressively select each CellGroup above the button:

Button["Select previous cell group", SelectionMove[EvaluationNotebook[], Previous, CellGroup]]

The documentation for the SelectionMove function provides quite a few examples, and shows the function can be used to select the internal contents of a cell as well as entire cells.

I think there's a way I can get Mathematica to give me the list of CellObject prior to the first of the SelectedCells but I'm not quite there yet.

share|improve this answer
Most@ExperimentalCellRange[First, EvaluationCell[]]. Or with only documented functions, TakeWhile[Cells[], # =!= EvaluationCell[] &] == Most@ExperimentalCellRange[First, EvaluationCell[]] – Rojo Dec 21 '12 at 11:12
Sorry, I thought it was before hte evaluating cell, use SelectedCell[] instead for the selected cell – Rojo Dec 21 '12 at 11:13
That's pretty cool, I'll need to play around with CellRange. I can't believe the Experimental context shows in the command completion menu now. – Martin John Hadley Dec 21 '12 at 11:46
@Rojo your first comment ate up a context backtick and the rest does not really work on V9 here... would you care to elaborate? – Yves Klett Dec 21 '12 at 18:49
Yes, sorry, Most@Experimental`CellRange[First, EvaluationCell[]]. Or with only documented functions, TakeWhile[Cells[], # =!= EvaluationCell[] &]. That should give a list of the CellObjects before the evaluating one. – Rojo Dec 21 '12 at 18:52

Your Answer


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.