JetBrains / IntelliJ keyboard shortcut to collapse all methods
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" .
Related videos on Youtube
Comments
-
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 almost 11 yearsYou may take a look at intellij code folding shortcuts. I guess that Ctrl+Shift+Minus is what you need.
-
Brad almost 11 yearsYEAHHHH. I didn't know it was called code folding so i couldn't find it. Thanks!
-
Vishy almost 11 yearsI 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 almost 11 yearsha. i would be too but it ain't my fault. I inherited this
-
kghastie over 6 yearsI 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 over 7 yearsThis is espacially useful if you use another keymap (such as the eclipse keymap).
-
John Pancoast over 6 yearsThis 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 over 6 yearsNice, 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 almost 6 yearsA command that does not recursively collapse everything inside the methods too would be nice.
-
Ali_Ai_Dev over 5 yearsCtrl + - : collapse current method
-
desertSniper87 over 5 yearsFor all non mac people
Cmd
isCtrl
andOption
isShift
-
Matt C over 5 yearsIntelliJ has a default keymap for this:
Ctrl
+NumPad-*
followed by the level of folding you want, entered on the NumPad. -
Varun Sharma almost 5 yearsGood to know about second stroke shortcuts.
-
Antimony over 4 yearsUnfortunately, "second stroke" shortcuts don't display the shortcut in the menu properly. See overthink's answer.
-
Amir Fo about 4 yearsIt collapses the blocks inside of methods! Isn't there any level 0 fold?
-
Angelos Pikoulas about 4 yearsThe answer below is much better, cause it has all the granularity I was looking for!
-
Northnroro about 4 yearsThe number on the numpad is working, but you need to release Ctrl+Shift+* first before pressing the numpad 2.
-
shashwat about 4 years@Ali_dev is there a way to do the same to all methods?
-
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 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 about 3 yearsWRONG 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 about 3 yearsAnyone 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 almost 3 yearsMaybe 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 over 2 yearsHow did you manage to reassign keyboard shortcut for this? I'm using intellij 2021.1.3 on macOs Catalina.
-
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 about 2 yearsIf 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 checkMethod Bodies
or if you only want this to apply to PHP scroll down to PHP and checkMethod body
andFunction body
then clickApply
. 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 about 2 yearsNote 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.