# Unexpected behavior of SelectionMove

Bug introduced in 11.2 or earlier and fixed in 12.0 [CASE:3968507]

I think this is a bug. If someone can help to confirm it, I'll report it to Wolfram. I can selet previous cell group:

SelectionMove[PreviousCell[], All, CellGroup]


But I fail to do this like follows

Maybe you will say that it is a Cell not a CellGroup, but why I can do this:

Can anyone give a reasonable explanation? You can get the .nb test file by run

NotebookPut[Uncompress[First[Values[Databin["fVOforSX"]]["nb"]]]]

• @Kuba I'm sorry,I couldn't still understand why I can run normally in third case as you say. – yode Sep 23 '16 at 11:00
• Ah, I'm sorry, I missed the point. This is the problem when one has to rewrite the code himself. Sorry again, it indeed looks like an inconsistency. ps. Don't attach notebooks, they are not safe in general so people are not going to download it very often. – Kuba Sep 23 '16 at 11:04
• Here is my guess: All CellGroup is trying to reach CellGroup from the bottom, it fails so there is no selection. Previous CellGroup is trying to find it from the "top", so it narrows down selection up to a single cell, it fails but the cell is already selected. Just a guess. – Kuba Sep 23 '16 at 11:08
• So I'd say All behaves as expected and Previous gives you too much. – Kuba Sep 23 '16 at 11:09
• Yep, you can ask WRI Support. – Kuba Sep 23 '16 at 11:37

This indeed looks like a bug. Citing the Documentation page for SelectionMove:

SelectionMove returns $Failed if it cannot move the selection in the way you request. In your example SelectionMove obviously fails but returns Null instead of $Failed what directly contradicts the Docs.

One workaround is to check whether the selection is empty (SelectionMove failed) and if so to attempt to select Cell instead of CellGroup:

SelectionMove[PreviousCell[], All, CellGroup];
If[SelectedCells[] === {},
SelectionMove[PreviousCell[], All, Cell]];


Another way is to rely upon the working functionality:

SelectionMove[PreviousCell[], Before, CellGroup]
SelectionMove[EvaluationNotebook[], Next, CellGroup]


(instead of EvaluationNotebook[] one can use ParentNotebook@PreviousCell[]).

• Reported to the support as [CASE:3968507]. – Alexey Popkov Nov 5 '17 at 9:11