2

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.

Kvothe
  • 4,419
  • 9
  • 28
  • Did you look at SelectionMove? That should allow you to select the cell group – Lukas Lang Jul 15 '18 at 12:03
  • 1
    This is currently built in. See Command-' on Mac or its corresponding bindings on other systems. – b3m2a1 Jul 15 '18 at 18:37
  • 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. – murray Jul 15 '18 at 19:31
  • @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. – Kvothe Jul 17 '18 at 00:16
  • @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. – Kvothe Jul 17 '18 at 00:19
  • @Kvothe I see. You do want something slightly different, but not much. You can click Control-. until you have the cell bracket you want to close and then Command+' will open/close it. Otherwise it'll be a bit of a hassle to add a nice keybinding. You'll have to edit the KeyEventTranslations.tr file and then restart the FE. Very annoying. – b3m2a1 Jul 17 '18 at 00:25
  • @b3m2a1, thanks. I'm the kind of person that would prefer to spend an hour (or a few hours) all at once on creating the keyboard binding over spending a few seconds every time I want to do this on pressing multiple buttons. Time spend doing the first I do not really consider wasted since I'll be learning something useful (there will be other key bindings I will want to create and I will be quicker the next time). Doing the second I do consider just a waste of time. – Kvothe Jul 17 '18 at 00:30
  • @Kvothe then look at this and make use of the token "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
  • I try not to touch .tr files anymore, is a solution based on https://mathematica.stackexchange.com/a/184568/5478 acceptable? – Kuba Jan 07 '19 at 07:34

0 Answers0