Is there any way for me to automatically select all cells that are completely empty? You know, those cells that you create but end up cutting what's in them and paste it somewhere else, so after a while you get a bunch of empty cells in your workbook (if you're sloppy as me at least).

My other question is, is there any way for you to automatically select all the cells matching a particular pattern (e.g. if I wanted to delete all cells that have Select[] inside), or that have a style (text cells).


  • $\begingroup$ Empty is a tricky thing here, there may be a lot of " " or "\n" and is it empty cell too? You can NotebookRead on cells but pattern approach for the content may be tricky to. What you see in notebook does not necessarily indicate unique box structure. But for detecting occurance of Select it should be enough. $\endgroup$
    – Kuba
    Nov 13 '14 at 11:03
  • $\begingroup$ You are right, I was only thinking on what I see in a notebook. Indeed, some cells often have "\n" and I don't see those, so maybe the search could be on white space? $\endgroup$
    – Sos
    Nov 13 '14 at 11:06

More stable answer, in case of images and stuff evaluated in place in input cells:

NotebookDelete /@ Select[Cells[GeneratedCell -> False], StringMatchQ[
      FrontEnd`ExportPacket[NotebookRead@#, "InputText"]],
    (" " | "\t" | "\r" | "\n" | "\[IndentingNewLine]") ...
    ] &

It may be slower than the original one.

Old answer

Would that be ok?

NotebookDelete /@ Select[
     Cells[EvaluationNotebook[], GeneratedCell -> False ],
       StringJoin @@ Flatten[List @ First[NotebookRead[#]] /. RowBox | BoxData -> List],
       Except[{" ", "\n", "\[IndentingNewLine]"}]] == 0 &
  • $\begingroup$ eh, it works, also on those with multiple paragraphs and stuff! $\endgroup$
    – Sos
    Nov 13 '14 at 11:22
  • $\begingroup$ @Sosi Great :) good luck then. $\endgroup$
    – Kuba
    Nov 13 '14 at 11:26
  • $\begingroup$ @Kuba, the updated solution doesn't seem to work. it doesn't delete the empty cellls. $\endgroup$
    – Saqib Ali
    Oct 5 '16 at 5:16
  • $\begingroup$ @SaqibAli could you copy an example of such cell's BoxData so I can diagnoze the problem? $\endgroup$
    – Kuba
    Oct 5 '16 at 5:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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