3
$\begingroup$

I've got a list of cells I want to select, but I'd like to do this without the selection cursor moving over each. Is there a way I can do this?

$\endgroup$
2
$\begingroup$

We can do this in a similar way to this. But the trick is simply to start with a FrontEnd`NotebookSuspendScreenUpdates on the notebook in question.

Here's a way to do this:

Options[selectCells] =
  Options[NotebookFind];
selectCells[cells : {__CellObject}, ops : OptionsPattern[]] :=

  With[{c = GroupBy[cells, ParentNotebook], tag = CreateUUID[]},
   KeyValueMap[
    CheckAbort[
      FrontEndExecute@
       FrontEnd`NotebookSuspendScreenUpdates[#];
      With[{nb = #},
       Map[
        Function[
         SelectionMove[#, All, Cell, AutoScroll -> False];
         FrontEndExecute@
          FrontEnd`SelectionAddCellTags[nb, {tag}];
         ],
        #2
        ]
       ];
      NotebookFind[#, tag, All, CellTags, ops];
      FrontEndExecute@
       FrontEnd`SelectionRemoveCellTags[NotebookSelection[#], tag];
      FrontEndExecute@
       FrontEnd`NotebookResumeScreenUpdates[#];,
      FrontEndExecute@
        FrontEnd`NotebookResumeScreenUpdates[#];
      ] &,
    c
    ];
   c
   ];
selectCells[cell_CellObject, ops : OptionsPattern[]] :=

 selectCells[{cell}, ops]

This we can use this on a list of cells and they will be selected, but without each being visibly selected in turn.

Note that we wrap it in a CheckAbort with a FrontEnd`NotebookResumeScreenUpdates so that if things go pear-shaped we don't lose our screen updating.

This can be made yet more useful, in fact, by including a function to map after the cell has been selected. In doing so one gets a function mapped over cell selections, but without it being visible what's happening.

| improve this answer | |
$\endgroup$
  • $\begingroup$ I'm not sure what is the goal, selectCells@Cells[][[;; 2]] leaves the selection on those two cells. By the way selectCells@First@Cells[] throws recursion error. $\endgroup$ – Kuba Aug 23 '17 at 20:39
  • $\begingroup$ And if the selection is moved at the end, that isn't this question the same? selecting non adjacent cells $\endgroup$ – Kuba Aug 23 '17 at 20:43
  • $\begingroup$ @Kuba oops, forgot to put the list in that prevents the recursion. It's similar but semantically it's different to me, in that it's a) about selecting a list of cells, not just to the end (because you can do the latter with "SelectNextLine") and b) it's about hiding the selection process, which is very useful -- panning through each selection looks really clumsy. I wouldn't mind if it's closed as a duplicate, but I think at the very least having it here is useful for those who don't think to search for "select to the end". $\endgroup$ – b3m2a1 Aug 23 '17 at 20:53
  • $\begingroup$ I linked different topic than you did. :) $\endgroup$ – Kuba Aug 23 '17 at 21:00
  • $\begingroup$ @Kuba hah, I totally missed that. Sorry. Yeah, it's essentially the same as that, except hiding what you're doing. $\endgroup$ – b3m2a1 Aug 23 '17 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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