Related to Delete the current notebook cell using the keyboard. Difference is that here I want to delete the whole group of cells, including the original Input cell and all Print, Output, Message etc. cells it generated (or belong to the same group). However in the case when several Input cells are grouped it will not delete the whole group, but only the subgroup that belongs to that one Input cell.


2 Answers 2


Here is the code that should be added to KeyEventTranslations.tr. To learn more about editing this, and similar, system files look at this question and its related.

  Command, d   -   deletes the current cell (or selected set of cells) and its (or their) related group(s) if the group has exactly 1 Input cell; otherwise deletes only the cell from selection
Item[KeyEvent["d", Modifiers -> {Command}],
    KernelExecute[Module[{nb = InputNotebook[], cl, st, pr, cr},
     SelectionMove[nb, All, Cell, AutoScroll -> False];
     MathLink`CallFrontEnd[FrontEnd`SelectionAddCellTags[nb, {"$forDeletion"}]];
	 While[NotebookFind[nb, "$forDeletion", Next, CellTags, AutoScroll -> False] =!= $Failed,
	  MathLink`CallFrontEnd[FrontEnd`SelectionAddCellTags[nb, {"$temporary"}]];
	  SelectionMove[nb, All, CellGroup, AutoScroll -> False];
	  If[(cl = Developer`CellInformation[nb]) === $Failed,
       NotebookFind[nb, "$temporary", Next, CellTags, AutoScroll -> False]
	   , For[pr = 0, And[(cr = Length[cl]) > pr, Length@Position[ReplaceAll["Style", cl], "Input"] == 0], pr = cr,
	      SelectionMove[nb, All, CellGroup, AutoScroll -> False];
	      cl = Developer`CellInformation[nb];
	   If[Length@Position[ReplaceAll["Style", cl], "Input"] =!= 1, NotebookFind[nb, "$temporary", Next, CellTags, AutoScroll -> False]]
      MathLink`CallFrontEnd[FrontEnd`SelectionRemoveCellTags[nb, {"$temporary", "$forDeletion"}]];
      MathLink`CallFrontEnd[FrontEnd`SelectionAddCellTags[nb, {"$deleted"}]]
	 If[NotebookFind[nb, "$deleted", All, CellTags, AutoScroll -> False] =!= $Failed,
	  MathLink`CallFrontEnd[FrontEnd`SelectionRemoveCellTags[nb, {"$deleted"}]];
    ]], MenuEvaluator -> Automatic
  Command, Alt, d   -   deletes only the current cell
Item[KeyEvent["d", Modifiers -> {Command, Option}],
     SelectionMove[SelectedNotebook[], All, Cell], 

This will create two keyboard shortcuts. Both of them are made in such a way that quite poor Mathematica's Undo command can be used! The user can choose which keys to assign to them.

  • The first one deletes the current group-cell, without affecting other cells in the notebook, as requested. In the case that more than one group is needed for deletion, just select any cell from each group and press the shortcut.
  • The second shortcut is for deleting one cell exclusively.

If you'd like to be able to do this without adding a custom keyboard shortcut, you can do it by repeatedly pressing Ctrl+. (this is the shortcut for Edit menu -> Extend Selection).

For those unfamiliar with the wonderful Extend Selection command, it is nearly equivalent to double/triple/quadruple mouse clicking to select ever-larger expressions, with 2 nice advantages:

  1. When pressing Ctrl+. repeatedly you can do so as slowly as you like, examining the result of the previous press each time.
  2. (This is the key for your question:) When your selection grows to contain the entire cell contents, the next press of Ctrl+. will select the cell itself, and subsequent presses will select ever larger cell groups, until finally the entire notebook is selected.

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.