Is there a shortcut to select all cells above the current cursor position?

One possibility is to modify your personal KeyEventTranslations.tr (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["http://www.mertig.com/shortcuts.m"]


    mymenuitems="
         (* Select all cells upwards *)
         Item[KeyEvent[\"Home\", Modifiers -> {Control, Shift}], 
         KernelExecute[
          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"]},
Quiet@CreateDirectory@FileNameJoin[{$UserBaseDirectory,"SystemFiles","FrontEnd","TextResources",os}];
    mykeyeventtrans=FileNameJoin[{$UserBaseDirectory,"SystemFiles","FrontEnd","TextResources",os,"KeyEventTranslations.tr"}];
    (*If[FileExistsQ[mykeyeventtrans],DeleteFile@mykeyeventtrans];*)
    If[!FileExistsQ[mykeyeventtrans],
    CopyFile[FileNameJoin[{$InstallationDirectory,"SystemFiles","FrontEnd","TextResources",os,"KeyEventTranslations.tr"}],mykeyeventtrans]
]];
keytext=Import[mykeyeventtrans,"Text"];
mykeytext=StringReplace[keytext,"EventTranslations[{":>StringJoin["EventTranslations[{\n(* User defined *)\n",mymenuitems,",\n"]];
Export[mykeyeventtrans,mykeytext,"Text"];

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

CreatePalette[
 Button["Evaluate above",
  With[{NB = InputNotebook[]},
   Do[ SelectionMove[Experimental`FromCellIndex[NB, i], All, Cell];
       SelectionEvaluate[NB];
       , {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.


This is a modification of @Kuba's answer, again evaluating all cells above and including the selected cell, but without the "beep" (a minor refinement). Thanks @Kuba for the main routine. As with the original, the code generates a dinky palette with a single "Evaluate Above" button, which may then placed at any convenient location (such as next to the other menu items at the top of your working notebook). A cell (any cell) must be selected for the "Evaluate Above" button to work.

CreatePalette[
 Button["Evaluate Above",
  With[{nbI = InputNotebook[]},
   Do[
    SelectionMove[Experimental`FromCellIndex[nbI, i], All, Cell];
     If[TextString["Style" /. Developer`CellInformation[nbI][[1]]] === "Input",
      SelectionEvaluateCreateCell[nbI]];,
     {i, 1, Experimental`ToCellIndex@SelectedCells[nbI][[1]]}]],Method -> "Queued"]]

The code change selects out only Input cells for evaluation, "skipping over" (generating a Null result) for all other cell styles. Thanks @John Fultz for the command that returns cell styles (under "How can I get the style of selected cells?"). Evidently, a "beep" would occur if non-input cells were asked to be evaluated. This seems to be working fine with Mathematica 11.0.1.

I'm new to Mathematica, and this thread has not been active for a while. So I'm not sure whether there is now some easy way to do this built in (can't find anything), or whether a custom routine like this is still required.

Update 3/10/18: I've added the Button option Method -> "Queued" in order to prevent the timeout that occurs with the default Button evaluation method "Preemptive." The "Queued" option is used to evaluate button functions on the main link, which never times out.

Tags:

Front End