11
$\begingroup$

Is there an easy way to select all initialization cells (and only those) in a Mathematica notebook?

$\endgroup$
2
  • $\begingroup$ If you just need to evaluate the initialization cells, this can be done via the Evaluation menu -> Evaluate Initialization Cells, or programmatically using FrontEndExecute[FrontEndToken["EvaluateInitialization"]]. $\endgroup$
    – dws
    Aug 8, 2012 at 16:54
  • $\begingroup$ @dws: Yes, that I know (and that's the main reason why I use them to begin with). However, every now and them I want to copy all the initialization cells (which due to my working style tend to be scattered throughout the notebook) into a fresh notebook (simply because the current notebook gets too large). And I'd like to avoid the tedious work of going through the notebook and looking for initialization cells. $\endgroup$
    – celtschk
    Aug 8, 2012 at 17:00

3 Answers 3

12
$\begingroup$

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.

$\endgroup$
23
  • $\begingroup$ That's an interesting solution, but, as far as I can see, unfortunately not practical for my case because I often decide whether a cell should be an initialization cell after I created it. That is, I create the cell normally, and then later if I notice that I use the definition again farther away, I change it to an initialization cell. $\endgroup$
    – celtschk
    Aug 8, 2012 at 16:54
  • $\begingroup$ It's what I did. I write codes in normal Input-style cells, and later select some of them and press alt+i to convert to Initial-style. This style definition is no more than just a wrap on a Input-style. $\endgroup$
    – Silvia
    Aug 8, 2012 at 16:57
  • 2
    $\begingroup$ Small note: new styles can inherit options from old styles. In this case, inheriting from the "Input" style may be desirable with StyleData["Initial", StyleDefinitions->StyleData["Input"]]. This makes cleaner and clearer definitions most of the time. $\endgroup$
    – dws
    Aug 8, 2012 at 18:09
  • 1
    $\begingroup$ @celtschk If the first cell contains the contents given by Silvia, I don't know what happened. You can start over with the default stylesheet from the Format menu. Then edit the (private) stylesheet and just copy-paste Silvia's code there below the "Inheriting..." cell. Say ok when MMA asks whether to interpret this. This should add the new style. Finally, you can save and install the stylesheet as described above. $\endgroup$
    – dws
    Aug 8, 2012 at 18:59
  • 2
    $\begingroup$ There is already a Code style that is an initialization cell. That has particular styling that may not appeal but in that case StyleDefinitions->StyleData["Code"] probably makes more sense (?) $\endgroup$ Aug 8, 2012 at 23:14
10
$\begingroup$

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
$\endgroup$
4
  • $\begingroup$ An interesting solution, definitely worth having around, although for new notebooks Silvia's solution is superior. $\endgroup$
    – celtschk
    Aug 8, 2012 at 18:06
  • $\begingroup$ @celtschk I agree. Too bad NotebookFind doesn't let you search cell options. $\endgroup$
    – dws
    Aug 8, 2012 at 18:11
  • $\begingroup$ +1, for an existing nb you would like to scan your cells programmaticly. $\endgroup$
    – Silvia
    Aug 8, 2012 at 18:37
  • $\begingroup$ +1. A consolation prize would be to use CurrentValue[EvaluationNotebook[], CellEvaluationFunction] instead of $Pre $\endgroup$
    – Rojo
    Aug 9, 2012 at 1:14
4
$\begingroup$

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}
  ]
$\endgroup$
3
  • 1
    $\begingroup$ Faster is to use Pick[Cells[], CurrentValue[Cells[], InitializationCell]] instead of Select. $\endgroup$
    – Carl Woll
    Aug 26, 2017 at 6:11
  • $\begingroup$ @CarlWoll Thanks, again. I usually use Pick, but I probably didn't realize CuurentVallue would be "listable" in this context and didn't think the performance difference would be significant. $\endgroup$
    – Michael E2
    Aug 26, 2017 at 12:18
  • $\begingroup$ This doesn't show the located cell if it is within some section. But one can open all subgroups before. $\endgroup$
    – BoLe
    Aug 7, 2020 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.