I want to delete current and previous cells.So I make a custom function:

dd := (SelectionMove[EvaluationCell[], Previous, CellGroup]; 
  NotebookDelete[]; NotebookDelete[EvaluationCell[]])

But I'm confused why examples below do not work:

  • First

    dd := (NotebookDelete[
       SelectionMove[EvaluationCell[], Previous, CellGroup]]; 
  • Second

    dd := (obj1 = SelectionMove[EvaluationCell[], Previous, CellGroup];
      obj2 = EvaluationCell[]; NotebookDelete[{obj1, obj2}];)

I can not check any error in my code. And I think the usage is supported by Mathematica:

  • $\begingroup$ I don't know what exactly is your point but notice that SelectionMove does not return CellObject (or anything in general). $\endgroup$
    – Kuba
    Commented Sep 22, 2016 at 8:45
  • $\begingroup$ @Kuba You are right maybe.So how to return that CellObject in current selection? $\endgroup$
    – yode
    Commented Sep 22, 2016 at 9:05
  • $\begingroup$ SelectedCells[] $\endgroup$
    – Kuba
    Commented Sep 22, 2016 at 9:06
  • $\begingroup$ @Kuba Thanks very much.You help me again. $\endgroup$
    – yode
    Commented Sep 22, 2016 at 9:07

1 Answer 1


Notice that SelectionMove does not return CellObject (or anything in general) that would be suitable for other FrontEnd related functions.

This is a problem with your examples, obj1 is really Null.

But you can get to know which cell was selected after SelectionMove, use SelectedCells[].

Here is a minimal example how would it work, create two dummy cells:



and evaluate

SelectionMove[First @ Cells[], All, Cell];
obj1 = SelectedCells[];
obj2 = PreviousCell[]; 
NotebookDelete[Flatten @ {obj1, obj2}];

Your Answer

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

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