How to select all initialization cells?

My method is to define a special style in stylesheet for initialization cells. Here is my definition:

Cell[StyleData["Initial"],
 CellFrame->{{1, 1}, {1, 1}},
 CellMargins->{{78, 10}, {5, 10}},
 Evaluatable->True,
 CellGroupingRules->"InputGrouping",
 TextClipboardType->"InputText",
 StripStyleOnPaste->True,
 PageBreakWithin->False,
 GroupPageBreakWithin->False,
 InitializationCell->True,
 DefaultFormatType->DefaultInputFormatType,
 ShowAutoStyles->True,
 "TwoByteSyntaxCharacterAutoReplacement"->True,
 HyphenationOptions->{"HyphenationCharacter"->"\[Continuation]"},
 AutoItalicWords->{},
 LanguageCategory->"Mathematica",
 FormatType->InputForm,
 NumberMarks->True,
 LinebreakAdjustments->{0.85, 2, 10, 0, 1},
 CounterIncrements->"Input",
 MenuCommandKey->"i",
 FontWeight->"Bold",
 Background->RGBColor[1., 1., 0.8666666666666667]]

Then you can use alt+i to insert an initialization cell / convert an existing cell to Initial-style, and use alt+left-click to select all cells of this style in current notebook.


Here is a method that can be run on existing notebooks, but has some drawbacks.

It leverages the Evaluate Initialization Cells option by redefining $Pre to set the CellTags then NotebookFind will select all the initialization cells.

First, we have a function that will set the CellTags of a cell when it is evaluated.

tagFun[input_] := (SelectionMove[InputNotebook[], All, EvaluationCell,
    AutoScroll -> False]; 
  SetOptions[NotebookSelection[], CellTags -> "init"]; 
  SelectionMove[InputNotebook[], After, Cell, AutoScroll -> False]; 
  input)

Then, change $Pre and evaluate only initialization cells.

oldpre = $Pre;
    $Pre = tagFun;
FrontEndExecute[FrontEndToken["EvaluateInitialization"]];

Finally, change $Pre back and select the initialization cells.

$Pre = oldpre;
NotebookFind[InputNotebook[], "init", All, CellTags]

The drawbacks are:

  • I could not package this into a function. In fact, the commands apparently need to be in separate cells (as I have them here).
  • This evaluates the initialization cells when run.
  • Overwrites CellTags on initialization cells

Here are a couple of new V9 approaches that use Cells. I think the Cells function makes some of these kinds of tasks simpler.


The first solution achieves the goal mentioned in a comment: How to copy all the initialization cells.

nb = EvaluationNotebook[]; (* change as desired *)
CopyToClipboard @ 
  NotebookRead @ Select[Cells[nb], CurrentValue[#, InitializationCell] &]

The cells may be pasted in another notebook. The Cells approach allows one to do many things with cells, even if they are not actually selected in the notebook.


This creates a palette that allows you to mover a slider to select each initialization cell in turn. It won't select all of them as asked for, but one can locate them, which sometimes I want to do. (Thanks to @CarlWoll for suggesting an improvement.)

CreatePalette @ Manipulate[
  initcells = Pick[#, CurrentValue[#, InitializationCell]] &@ Cells[SelectedNotebook[]];
  whichCell = Clip[whichCell, {0, Length[initcells]}];
  If[whichCell > 0, SelectionMove[#[[whichCell]], All, Cell] &@initcells];
  whichCell,

  {whichCell, 0, Length[initcells], 1},
  {{initcells, {}}, None},
  TrackedSymbols :> {whichCell}
  ]

Tags:

Front End