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.
SelectionMove? That should allow you to select the cell group – Lukas Lang Jul 15 '18 at 12:03Plotexpression 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. – murray Jul 15 '18 at 19:31Control-.until you have the cell bracket you want to close and thenCommand+'will open/close it. Otherwise it'll be a bit of a hassle to add a nice keybinding. You'll have to edit theKeyEventTranslations.trfile and then restart the FE. Very annoying. – b3m2a1 Jul 17 '18 at 00:25"OpenCloseGroup". It should take about 5 minutes, but it'll dirty the selection, force a restart, and likely be a little bit fragile. – b3m2a1 Jul 17 '18 at 00:34