# Fold/unfold all groups at given level via keyboard shortcut

The combination ctrl+' can be used to fold/unfold a selected group.

Is there a way to fold/unfold all cells at a given nesting level, and possibly also to fold/unfold all groups? I'm thinking something along the lines of ctrl+k ctrl+j, ctrl+k ctrl+1 and so on, that are used for these kinds of folding actions in Visual Studio Code, Atom, and possibly other editors.

Given that in a Mathematica notebook the "nesting" works different than in a regular code editor (no nesting inside code for one), by "nesting level" I here mean to fold/unfold all chapters, sections, subsections and so on.

Edit: I've just found this other post where Kuba gives a function to open/close all groups at a given level. It therefore only remains to find a way to bind this to the appropriate keyboard shortcuts.

I'm however not sure how to use the above mentioned bindings to do this.

• You could iterate the folding process (via the appropriate front-end token) then put that iteration into your KeyEventTranslations.tr – b3m2a1 Aug 27 '17 at 16:53
• @b3m2a1 possibly, but my first problem with that is that I cannot find the appropriate front-end token to use. Strangely enough, searching KeyEventTranslations.tr for ' or words related to grouping/folding doesn't give me anything – glS Aug 27 '17 at 16:55
• That's in MenuSetup.tr (most of the key-events are). – b3m2a1 Aug 27 '17 at 16:56
• thanks, found it. So the token seems to be OpenCloseGroup. Still I cannot seem to make it work from the front end. Something like FrontEndExecute@FrontEndToken[EvaluationNotebook[], "OpenCloseGroup"] does nothing – glS Aug 27 '17 at 17:03
• Try this: SelectionMove[PreviousCell[], All, CellGroup]; FrontEndTokenExecute[ParentNotebook@PreviousCell[], "OpenCloseGroup"] – b3m2a1 Aug 27 '17 at 17:04