JetBrains / IntelliJ keyboard shortcut to collapse all methods

136,944

Solution 1

You may take a look at intellij code folding shortcuts.

For Windows/Linux do: Ctrl+Shift+-

For mac use Command+Shift+-

To unfold again do Ctrl+Shift++ or Command+Shift++ respectivley.

Solution 2

The above suggestion of Ctrl+Shift+- code folds all code blocks recursively. I only wanted to fold the methods for my classes.

Code > Folding > Expand all to level > 1

I managed to achieve this by using the menu option Code > Folding > Expand all to level > 1.

I re-assigned it to Ctrl+NumPad-1 which gives me a quick way to collapse my classes down to their methods.

This works at the 'block level' of the file and assumes that you have classes defined at the top level of your file, which works for code such as PHP but not for JavaScript (nested closures etc.)

Solution 3

go to menu option Code > Folding to access all code folding related options and their shortcuts.

Solution 4

@precastic's answer above is, imo, the right idea.

Worth noting that in IDEA 2018.2 (and surely other nearby versions) there are default keyboard shortcuts for this: (showing Mac, see Code > Folding > Expand All to Level for your system):

Cmd+Option+Keypad *, 1 - expand all to level 1
Cmd+Option+Keypad *, 2 - expand all to level 2
...
Cmd+Option+Keypad *, 5 - expand all to level 5

Note: these are "second stroke" shortcuts. First press Cmd+Option+*, then release, then hit the number you want.

Solution 5

You Can Go To setting > editor > general > code folding and check "show code folding outline" .

Share:
136,944

Related videos on Youtube

Brad
Author by

Brad

Developer :)

Updated on July 17, 2022

Comments

  • Brad
    Brad almost 2 years

    I'm working on some legacy code that has a class that is 10,000+ lines of code and has 100s of methods. Is there a shortcut for any JetBrains IDE (since the shortcut would likely be shared across all of them) to collapse all the methods / functions so that only the method signatures are shown?

    Something like this:

    public String myMethod(String arg1, int arg2){...}
    
    public String mySecondMethod(String arg1, int arg2){...}
    
    • lifus
      lifus almost 11 years
      You may take a look at intellij code folding shortcuts. I guess that Ctrl+Shift+Minus is what you need.
    • Brad
      Brad almost 11 years
      YEAHHHH. I didn't know it was called code folding so i couldn't find it. Thanks!
    • Vishy
      Vishy almost 11 years
      I would be tempted to downvote except I almost cried when you said "a class that is 10,000+ lines of code and has around 100s of methods" so +1 for trying.
    • Brad
      Brad almost 11 years
      ha. i would be too but it ain't my fault. I inherited this
    • kghastie
      kghastie over 6 years
      I realize this doesn't answer the question as asked, but Ctrl-F12 will open a navigator to hop between methods, etc. (You can narrow down the list of methods by typing in this Structure View as well.) The Structure Pane (Alt-7) adds more options. This might be a more effective way of exploring files with large numbers of methods, so I leave the suggestion here for future googlers.
  • Magnilex
    Magnilex over 7 years
    This is espacially useful if you use another keymap (such as the eclipse keymap).
  • John Pancoast
    John Pancoast over 6 years
    This should be the accepted answer IMO. It solves the problem in 1 step and solves it exactly as asked. If instead you the do the accepted answer you will be folding everything that can be folded and then unfolding to see your methods. I've been doing the latter and it's not that time consuming, but it's nice to know I can skip some steps with the former from this post. Thanks!
  • CodeGodie
    CodeGodie over 6 years
    Nice, this does it. The only problem im facing now is I cant do it by pressing keyboard buttons as I have a MacbookPro with no numpad. Will have to Google on how to assign custom keys i suppose. Sigh.
  • trinity420
    trinity420 almost 6 years
    A command that does not recursively collapse everything inside the methods too would be nice.
  • Ali_Ai_Dev
    Ali_Ai_Dev over 5 years
    Ctrl + - : collapse current method
  • desertSniper87
    desertSniper87 over 5 years
    For all non mac people Cmd is Ctrl and Option is Shift
  • Matt C
    Matt C over 5 years
    IntelliJ has a default keymap for this: Ctrl + NumPad-* followed by the level of folding you want, entered on the NumPad.
  • Varun Sharma
    Varun Sharma almost 5 years
    Good to know about second stroke shortcuts.
  • Antimony
    Antimony over 4 years
    Unfortunately, "second stroke" shortcuts don't display the shortcut in the menu properly. See overthink's answer.
  • Amir Fo
    Amir Fo about 4 years
    It collapses the blocks inside of methods! Isn't there any level 0 fold?
  • Angelos Pikoulas
    Angelos Pikoulas about 4 years
    The answer below is much better, cause it has all the granularity I was looking for!
  • Northnroro
    Northnroro about 4 years
    The number on the numpad is working, but you need to release Ctrl+Shift+* first before pressing the numpad 2.
  • shashwat
    shashwat about 4 years
    @Ali_dev is there a way to do the same to all methods?
  • Ali_Ai_Dev
    Ali_Ai_Dev about 4 years
    @shashwat not exactly. you can use Ctrl+Shift+Minus to collapse all of them and then use Ctrl+Alt+Plus to open one of that recursively.
  • Josh C
    Josh C over 3 years
    @Ali_dev thanks, the old shortcuts don't work the same anymore. But with the ctrl alt plus gets the desired effect since ctrl shift minus now does a method-level recursive collapse
  • Steve Horvath
    Steve Horvath about 3 years
    WRONG ANSWER - see the one below, that's the correct one. This will fold up other code segments inside the methods, making reviewing code a pita.
  • Steve Horvath
    Steve Horvath about 3 years
    Anyone knows how to set shortcuts to ctlr/shift/num1 ? I'd like to do that, but phpstorm interprets it as ctrl/end for some reason (ctrl/num1 works, interestingly, adding the shift is the problem)
  • swade
    swade almost 3 years
    Maybe it's the fact that I'm using Goland and this is still the most appropriate response I could find, but the below answer doesn't work. This answer works better for Goland, IMO.
  • Sumit Kumar
    Sumit Kumar over 2 years
    How did you manage to reassign keyboard shortcut for this? I'm using intellij 2021.1.3 on macOs Catalina.
  • Hunter
    Hunter about 2 years
    @SumitKumar and Steve Horvath I'm on a mac using PhpStorm 2021.3.3. You can edit, remove, and update the key bindings by opening Preferences > Keymap. Once you've got that open you can search for "folding" and see what the default mappings are. You can right click on them to add additional key combos or change/remove the defaults.
  • Hunter
    Hunter about 2 years
    If you don't want to use keyboard shortcuts or the menu every time you can also update the preferences to do this by default when viewing files. Open Preferences > Editor > General > Code Folding from there under general you can check Method Bodies or if you only want this to apply to PHP scroll down to PHP and check Method body and Function body then click Apply. For me this ran on all the open tabs I had as well as files opened afterwards. macOS 12.3.1 | PhpStorm 2021.3.3
  • JoeMjr2
    JoeMjr2 about 2 years
    Note that this also works on a code selection. Select a block of code that contains methods, press Ctrl+Shift+Minus, and all the methods within the selection will be individually folded to one line each.