Function Repository Resource:

CellGroupMove

Source Notebook

Move a cell and its children before or after an adjacent cell or group at the same level

Contributed by: Noah Hardwicke

ResourceFunction["CellGroupMove"][Up]

moves the selected cell and its children in InputNotebook above the previous cell or group at the same level.

ResourceFunction["CellGroupMove"][Down]

directs the movement below the next cell or group at the same level.

ResourceFunction["CellGroupMove"][obj,dir]

moves the cell associated with the CellObject obj, or the selected cell associated with the NotebookObject obj.

Details and Options

ResourceFunction["CellGroupMove"] moves cells by deleting the originals and writing copies in a new location. Therefore the cell objects of the new cells will be different than those of the originals.
ResourceFunction["CellGroupMove"] returns a list of CellObject expressions for the moved cells, or $Failed if movement was not possible.
Unless the option setting AutoScrollFalse is given, the front end will scroll a notebook so that the result of ResourceFunction["CellGroupMove"] is visible.
ResourceFunction["CellGroupMove"] can create two kinds of potentially unexpected behavior:
If ResourceFunction["CellGroupMove"][obj,Up] moves a cell above the parent cell of its enclosing group, then ResourceFunction["CellGroupMove"][obj,Down] will not necessarily move the cell back to its original place. Instead, the cell will be moved below its former parent cell and any of the parent's children:

If a cell moves directly above another of lower grouping precedent, then the lower precedent cell will become grouped and move with its new parent:

Examples

Basic Examples

Move the next cell and its child to below the cell group after it:

Item 1
Subitem 1
Item 2
Subitem 2


Create a notebook and select its first cell:

In[1]:=
nb = NotebookPut[Notebook[{
     Cell["Section 1", "Section"], Cell["Text", "Text"], Cell["Section 2", "Section"], Cell["More text", "Text"], Cell[BoxData[RowBox[{"1", "+", "1"}]], "Input"], Cell[BoxData["2"], "Output"]}]];
SelectionMove[nb, Next, Cell]

Move the selected cell and its children to below the next group:

In[2]:=
ResourceFunction["CellGroupMove"][nb, Down]
Out[2]=


Create a palette to move cells up or down in the input notebook:

In[3]:=
CreatePalette[
  Column[{
    Button["Up", ResourceFunction["CellGroupMove"][Up]],
    Button["Down", ResourceFunction["CellGroupMove"][Down]]}]];

Publisher

Noah Hardwicke

Requirements

Wolfram Language 14.0 (January 2024) or above

Version History

  • 1.0.0 – 26 February 2025

Related Resources

License Information