8
$\begingroup$

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).

Thanks

$\endgroup$
2
  • $\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, 2014 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, 2014 at 11:06

1 Answer 1

10
$\begingroup$

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

NotebookDelete /@ Select[Cells[GeneratedCell -> False], StringMatchQ[
    First@
     FrontEndExecute[
      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 ],
     StringCount[
       StringJoin @@ Flatten[List @ First[NotebookRead[#]] /. RowBox | BoxData -> List],
       Except[{" ", "\n", "\[IndentingNewLine]"}]] == 0 &
     ]
$\endgroup$
4
  • $\begingroup$ eh, it works, also on those with multiple paragraphs and stuff! $\endgroup$
    – Sos
    Nov 13, 2014 at 11:22
  • $\begingroup$ @Sosi Great :) good luck then. $\endgroup$
    – Kuba
    Nov 13, 2014 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, 2016 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, 2016 at 5:25

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.