26

I'd love to have a way of IntelliJ letting me fold for loops and if/else statements. But, I can't seem to find a way to do it automatically. I know you can use the //region and //endregion comments, or that you can do it manually with Ctrl+Shift+period, but is there a way to avoid all that and have it fold like methods or classes, without doing all that other stuff?

3
  • 2
    you mean like ctrl +shift + - to fold all ? or ctrl +shift + + to expand? - you can also ctrl + + to expand the current scope or ctrl + - to fold the current scope.
    – blurfus
    Commented Apr 14, 2016 at 22:23
  • have a look at stackoverflow.com/a/50700010/3496570 Commented Jun 5, 2018 at 12:23
  • I wanted this feature but didn't know that it already existed. This was mainly because I relied on the GUI symbol for folding code. But a GUI symbol showing code folding option does not exist for these if, while etc...
    – Kathir
    Commented Dec 4, 2018 at 17:27

3 Answers 3

23

You can fold and unfold:

  • Code blocks, i.e. code fragments surrounded by a matched pair of curly braces {}.

    To collapse a code block, place the cursor within that block and then select Code | Folding | Fold Code Block or press ++. (mac) or ctrl++. (Linux / Windows).

Note Code folding works for the keywords if/ while/ else/ for/ try/ except/ finally/ with in case of at least two statements.

See: https://www.jetbrains.com/help/idea/2016.3/code-folding.html

3
  • 11
    That's a bit Mac-centric. For the rest of us, you can try Ctrl-Shift-. (Ctrl-Shift-period)...
    – Lambart
    Commented Jan 25, 2017 at 19:36
  • 6
    But how can you display toggles on the left for such code blocks? i.e. how can you fold code the block with mouse only? toggles appear only for comments and method bodies, but not for code blocks (if/ while/ else/ for/ try/ except/ finally/ with)
    – Dmitry
    Commented Mar 21, 2017 at 19:33
  • 1
    It seems language dependent. My Javascript is folding on blocks - any block i.e surrounded by curly braces { } - my .json too - but Java is folding on less stuff - e.g. functions, comments, imports etc.. Not blocks of code. Commented Sep 29, 2018 at 19:12
9

Under Editor > General > Code Folding, you can enable this functionality expressly.

enter image description here

By default, if you wish for it to collapse method bodies, you can tick that selection. There are a lot of other options available, which should cover your needs.

If there's something that isn't covered, you can always enable "Show code folding outline" and use that to fold certain elements, although loops aren't foldable.

2
  • 21
    "although loops aren't foldable" Well, guess that answers my question. I know of this settings page, and there isn't a setting for what I want, which is to be able to fold loops and if/else statements (as said it the OP). Guess I'll have to stick with folding it manually. Commented Apr 15, 2016 at 16:03
  • @PhotonicPizza were you able to somehow fold if/loops? Why would they not be foldable in 1st place !? :- (
    – Dariusz
    Commented Nov 17, 2018 at 12:50
0

Put the cursor anywhere in the line of method definition for which you want the code fragments to be folded and press ctrl+shift+- this will collapse the whole method and press ctrl++ right after. this will keep the if and for loop fragments inside the method collapsed (intellij 2020.1.1 linux)

Not the answer you're looking for? Browse other questions tagged or ask your own question.