0
$\begingroup$

I want to create a keyboard shortcut to open or close the section my cursor is currently in, or in case I am selecting a cell or multiple cells (let's assume not headers (alt 1-6) them self for simplicity*), it should close the containing section(s).

I'm having trouble finding a way to select all cells under the same header as the selected cell. LukasLang pointed out this can be done with SelectionMove using the option CellGroup from which I think the solution can be constructed (I will update with answer later).


I have not gotten a shortkey in KeyEventTranslations.tr to work but I have been using the code below with the "joker" from Customize keybindings without modifying files in the installation directory to make

Module[{enb=SelectedNotebook[]},
If[(enb=!=$Failed)&&Length[Cells[enb]] > 0,
(*Iff the cursur is in between cells, move down one cell: *)
    If[SameQ[CurrentValue[enb, "CellCount"] ,0], SelectionMove[enb, Previous, Cell, AutoScroll -> False]];
    SelectionMove[SelectedCells[][[1]],All,CellGroup];
    FrontEndTokenExecute["SelectionCloseAllGroups"];
,
Print["bug"]]
]

This has been working satisfactory but I would still prefer a shortkey using KeyEventTranslations.tr, but when I try that I somehow can't get the references to the notebook to work properly (it is some time ago that I tried, so I don't remember the exact problem).

Also if it could also reopen cells would be nice, but I find that that is not actually that essential.

$\endgroup$
  • $\begingroup$ Did you look at SelectionMove? That should allow you to select the cell group $\endgroup$ – Lukas Lang Jul 15 '18 at 12:03
  • $\begingroup$ This is currently built in. See Command-' on Mac or its corresponding bindings on other systems. $\endgroup$ – b3m2a1 Jul 15 '18 at 18:37
  • $\begingroup$ Command-' does not do what the question asked. E.g., if you have a Section and under that an Input cell containing a Plot expression that you evaluate to obtain a corresponding Output cell, and if either that Input cell or Output cell is selected and you press the key combination, it just collapses the Input-Output pair to show the one of the two that was not selected; it does not close the entire section. $\endgroup$ – murray Jul 15 '18 at 19:31
  • $\begingroup$ @LukasLang, thanks with SelectionMove and the option CellGroup I think I can construct something like I want. I will update when I have something that works satisfactory. $\endgroup$ – Kvothe Jul 17 '18 at 0:16
  • $\begingroup$ @b3m2a1, I can't find the exact thing you are referring to (link?). Is it what in my documentation shows up as ctrl + '. In that case it does something different from what I want, I'll update the question to be more clear. $\endgroup$ – Kvothe Jul 17 '18 at 0:19

Your Answer

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

Browse other questions tagged or ask your own question.