Message Boards Message Boards

0
|
5058 Views
|
14 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Is it possible to fold and unfold cells using keyboard shortcuts?

Posted 2 years ago

I would like to be able to 'fold' my functions so that only the first line appears, and then be able to unfold them again, all via the keyboard. Setting a cell to Open or not Open, sort of gives the effect I want, although I would prefer to be able to see the first line of the code.

I have seen reference to using ctrl + ' but using this on on MacOS with the latest V13 doesn't seem to do anything. Is there a setting I need to enable somewhere, or is there a totally different approach I should be using?

Cheers
Andy

POSTED BY: Andrew Burnett
14 Replies
Posted 2 years ago

Thanks Hans. I had never played with the .wl editor. That is actually an interesting approach. I will definitely try using that more. Being able to jump between functions is very helpful.

Cheers, Andy

POSTED BY: Andrew Burnett
Posted 2 years ago

Thanks Eric, that seems like a good collection of keystrokes to get me started.

Cheers Andy

POSTED BY: Andrew Burnett
Posted 2 years ago

Perhaps you could explore an alternative. Do the coding in .wl files instead of .nb files. There you will have a popup menu showing the functions and their expected arguments.

enter image description here

Attachments:
POSTED BY: Hans Milton
Posted 2 years ago

see the signatures of all my functions without having to scroll through a whole bunch of code

I know this isn't the same thing, but there is a function for seeing definitions associated with a symbol: Information. There is a special prefix form: ??. So you can evaluate this whenever/wherever you want:

??myFunction

Just using the groupings won't show you the signatures unless you manually keep your header text in sync with the function it relates to, but Information will tell you pretty much everything you want to know.

POSTED BY: Eric Rimbey
Posted 2 years ago

So, I think you've got the basic idea, but just in case it might help:

  • In addition to Command-' there are Command-{ and Command-}. These close/open all of the nested groups as well.
  • Control-. (period) will expand the selection around the cursor/current-selection. This can be repeated all the way up to include the entire notebook.
  • You can create your own groupings with Control-g (once you've selected the cells to be grouped).
  • You can create a new cell of a specific type with Control-n where n is a number (you can discover the mappings by looking at the Format->Style menu).
  • You can select all cells of the same type by holding Option while clicking on the bracket (yeah, I don't know of a shortcut for this, sorry).

So, putting these together gets pretty close to mouseless, but admittedly there are some mouse-required bits remaining. I rarely make my own groupings, but I use the others all the time to organize and clean up my notebook. The last one might seem weird, but occasionally I just want to delete all output cells, and this is the way I do it.

POSTED BY: Eric Rimbey
Posted 2 years ago

Thanks, I think I have this working now. However, even though it works, it doesn't seem to do quite what I need. The Cmd ' seems to fold the output cell, but not the input cell. What I am hoping is that there is a way to do something like this.

Given a function such as: myFunction[x_] := Module[ function; function];

I am hoping there is a way to collapse the cell so that it looks like myFunction[x_] ...

That would let me see the signatures of all my functions without having to scroll through a whole bunch of code.

Of course, the sections and sub-sections might be the way to achieve what I am looking for, but perhaps there is something like this built into the notebook interface?

POSTED BY: Andrew Burnett
Posted 2 years ago

Yes, it did. Cursor somewhere in the "header" cell. Then Ctrl + ' . Bracket selection not needed.

POSTED BY: Hans Milton
Posted 2 years ago

Yes, and Command ' on Mac also works, but what I am struggling with is: 1. It only seems to fold away the result of evaluating a cell 2. It seems as though I need to have the bracket selected for this to work. I.e. just having my cursor on the code doesn't result in the Command ' working. I am wondering if there is a notebook setting that I have to set somewhere. Did this just work out of the box for you?

POSTED BY: Andrew Burnett
Posted 2 years ago

The keyboard shortcut Ctrl + ' works for me on Windows. No mouse needed.

POSTED BY: Hans Milton
Posted 2 years ago

Thank you both. I am looking for a mouse-less approach, so I will give the sections mechanism.

Cheers Andy

POSTED BY: Andrew Burnett
Posted 2 years ago

Just double-click the enclosing cell bracket?

enter image description here

Documentation link

POSTED BY: Hans Milton
Posted 2 years ago

Well, it can be done easily, just not automatically (as far as I know). Use Sections/Subsections/etc and expand/collapse the groups. This is pretty much identical to what IDEs do, but they can parse the text to determine where the blocks are, whereas you'll need to decide for yourself where the folding should happen and manually use the appropriate cell type. This is my standard practice when a notebook starts to feel unwieldy.

I believe there are plugins for Mathematica for other IDEs (e.g. IntelliJ or Atom I think--going on memory here). I suspect the trade-offs are extreme.

POSTED BY: Eric Rimbey
Posted 2 years ago

Hi Eric, Thanks for your suggestions. The problem that I am trying to solve is that I am am becoming a little overwhelmed by all the functions I have defined in a relatively small program. Lots of editors let you jump between functions, but only show the function signature. Usually the function is collapsed, and you can pop it open when you want to edit it.

It's OK if it can't be done easily. Maybe it means that I need to move to a different editor once I get to this stage

POSTED BY: Andrew Burnett
Posted 2 years ago

I am not aware of any such setting for Cells, and I didn't find anything in the options inspector. But I also may be misunderstanding. How would you edit a cell that only showed the first line (assuming there was stuff in the cell below the fold)? Do you want to be folding/unfolding every time you edit? How should the width of the notebook influence what shows in a folded cell? Is there actually some further objective that you're trying to achieve that you haven't told us about?

Like maybe you want to know where a symbol is defined but you want to hide the definition. You could do this by putting a text cell just before the input cell where the definition is. The text cell could contain the symbol name and you could hide (close) the input cell. Or, better yet, use a cell type that has grouping rules (e.g. Section) instead of a text cell. Then, instead of hiding the input cell, you could just close the group.

POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract