How to collapse all methods in Xcode?
Asked Answered
O

7

240

How to collapse all methods in a class in Xcode?

Collapsing one by one is not an option anymore.

Ormond answered 14/5, 2010 at 13:49 Comment(2)
You can download a free xcode shortcut cheat sheet here. pragprog.com/screencasts/v-mcxcode/becoming-productive-in-xcodeParmenter
@Parmenter unfortunately after ten years it's not more valid :(Simaroubaceous
I
146

Updates in Xcode 10

Xcode 10 has increased support for code folding, including:

  1. A new code folding ribbon showing all of the multi-line foldable blocks of code in the editor
  2. A new style for folded code in the editor that allows you to edit lines with folded code
  3. Support for folding any block of code enclosed in curly braces
  4. Support for folding blocks of code from the folding ribbon, from structured selection, or from the

Menubar ► Editor ► Code Folding ► Fold menu item

enter image description here

Look at this snapshot:

enter image description here


Code folding was disabled in Xcode 9 beta 1, which is working now, in Xcode 9 Beta5 according to beta release note: Resolved in Xcode 9 beta 5 – IDE

Here is how:

  1. Press and hold (command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
  2. Keep (hold) (command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Fold option.
  3. Select Fold from menu list. It will fold your code and shows 3 dots, folding/covering entire block.
  4. Now, to again unfold your code block, release (command) button and click on 3 dots folding a block.

For easy understanding, look at this snapshot:

enter image description here


It's all keyboard short cuts are also working.
Fold                          ⌥ ⌘ ←      option + command + left arrow
Unfold                        ⌥ ⌘ →      option + command + right arrow
Unfold All                    ⌥ U        option + U
Fold Methods & Functions      ⌥ ⌘ ↑      option + command + up arrow
Unfold Methods & Functions    ⌥ ⌘ ↓      option + command + down arrow
Fold Comment Blocks           ⌃ ⇧ ⌘ ↑    control + shift + command + up
Unfold Comment Blocks         ⌃ ⇧ ⌘ ↓    control + shift + command + down
Focus Follows Selection       ⌃ ⌥ ⌘ F    control + option + command + F
Fold All                      ⌘ ⌥ ⇧ ←    command + option + shift + left
Unfold All                    ⌘ ⌥ ⇧ →    command + option + shift + right

**Code folding options from Xcode Menu**: > Menubar ▶ Editor ▶ Code Folding ▶ "Here is list of code folding options"

Here is ref snapshot:

enter image description here

Same options from Xcode Short-cut list:

Menubar ▶ Xcode ▶ Preferences ▶ Key Bindings ▶ "Here is list of code folding short-keys"

enter image description here

Incompetent answered 3/9, 2017 at 4:54 Comment(4)
I've found code folding to be buggy in trying to unfold by just clicking the ellipses. Only 7 out of 10 times does it work correctly. Other times I have to unfold all from the editor menu option.Kaiserism
not sure what im doing wrong but command option shift left does not fold allWilled
Unfold All appears twice in your shortcuts listInterclavicle
This approach also works in Xcode 13.Medial
F
500

As of Xcode 4 it seems to have changed.

command-alt-shift-left arrow will do the trick...

To fold/unfold current methods or if structures use:

Fold: command-alt-left arrow

Unfold: command-alt-right arrow

Freiman answered 15/2, 2011 at 14:24 Comment(2)
And also Command + Option + left collapse the current block. Use the same combinations with right key to expandOrmond
Nice trick! This is gonna save me so much time. It's working in Xcode 6.1.1Indomitable
I
146

Updates in Xcode 10

Xcode 10 has increased support for code folding, including:

  1. A new code folding ribbon showing all of the multi-line foldable blocks of code in the editor
  2. A new style for folded code in the editor that allows you to edit lines with folded code
  3. Support for folding any block of code enclosed in curly braces
  4. Support for folding blocks of code from the folding ribbon, from structured selection, or from the

Menubar ► Editor ► Code Folding ► Fold menu item

enter image description here

Look at this snapshot:

enter image description here


Code folding was disabled in Xcode 9 beta 1, which is working now, in Xcode 9 Beta5 according to beta release note: Resolved in Xcode 9 beta 5 – IDE

Here is how:

  1. Press and hold (command) button in keyboard and move/hover mouse cursor on any (start or end) braces. It will automatically highlight, block area.
  2. Keep (hold) (command) button in pressed condition and click on highlighted area. It will enable quick menu popover window with Fold option.
  3. Select Fold from menu list. It will fold your code and shows 3 dots, folding/covering entire block.
  4. Now, to again unfold your code block, release (command) button and click on 3 dots folding a block.

For easy understanding, look at this snapshot:

enter image description here


It's all keyboard short cuts are also working.
Fold                          ⌥ ⌘ ←      option + command + left arrow
Unfold                        ⌥ ⌘ →      option + command + right arrow
Unfold All                    ⌥ U        option + U
Fold Methods & Functions      ⌥ ⌘ ↑      option + command + up arrow
Unfold Methods & Functions    ⌥ ⌘ ↓      option + command + down arrow
Fold Comment Blocks           ⌃ ⇧ ⌘ ↑    control + shift + command + up
Unfold Comment Blocks         ⌃ ⇧ ⌘ ↓    control + shift + command + down
Focus Follows Selection       ⌃ ⌥ ⌘ F    control + option + command + F
Fold All                      ⌘ ⌥ ⇧ ←    command + option + shift + left
Unfold All                    ⌘ ⌥ ⇧ →    command + option + shift + right

**Code folding options from Xcode Menu**: > Menubar ▶ Editor ▶ Code Folding ▶ "Here is list of code folding options"

Here is ref snapshot:

enter image description here

Same options from Xcode Short-cut list:

Menubar ▶ Xcode ▶ Preferences ▶ Key Bindings ▶ "Here is list of code folding short-keys"

enter image description here

Incompetent answered 3/9, 2017 at 4:54 Comment(4)
I've found code folding to be buggy in trying to unfold by just clicking the ellipses. Only 7 out of 10 times does it work correctly. Other times I have to unfold all from the editor menu option.Kaiserism
not sure what im doing wrong but command option shift left does not fold allWilled
Unfold All appears twice in your shortcuts listInterclavicle
This approach also works in Xcode 13.Medial
U
22

X-Code 7 onwards

To collapse all method : Shift + Option + Command + Left arrow

To expand all method : Shift + Option + Command + Right arrow

Unmannerly answered 6/8, 2016 at 8:56 Comment(1)
what if i want only one method?Contrabassoon
Y
11

I like this table from Krunal's answer (above) : https://mcmap.net/q/116635/-how-to-collapse-all-methods-in-xcode

Fold                          ⌥ ⌘ ←      option + command + left arrow
Unfold                        ⌥ ⌘ →      option + command + right arrow
Fold Methods & Functions      ⌥ ⌘ ↑      option + command + up arrow
Unfold Methods & Functions    ⌥ ⌘ ↓      option + command + down arrow
Fold Comment Blocks           ⌃ ⇧ ⌘ ↑    control + shift + command + up
Unfold Comment Blocks         ⌃ ⇧ ⌘ ↓    control + shift + command + down
Focus Follows Selection       ⌃ ⌥ ⌘ F    control + option + command + F
Fold All                      ⌘ ⌥ ⇧ ←    command + option + shift + left
Unfold All                    ⌘ ⌥ ⇧ →    command + option + shift + right
Yt answered 15/9, 2021 at 12:50 Comment(0)
O
10

Looks like Ctrl-Command-Up does it (or on the menu: Editor-CodeFolding-FoldMethods/Functions).

Ctrl-Command-Down or Ctrl-U to reverse.

Osterman answered 14/5, 2010 at 13:55 Comment(0)
C
2
  1. Press COMMAND + , "command comma"

  2. Click "Text Editing" tab

  3. Check "Code Folding Ribbon"

Just to the right of line numbers you will see a new ribbon with up or down arrows next to functions / methods. Pressing these will allow you to expand or collapse functions / methods.

Cachou answered 18/3, 2021 at 9:50 Comment(0)
P
1

Shift + Option + Command + Left arrow, did not work for me. A work around I have found if use "Find" and put in "[". use "Return" to skip to arrays. When you come to one you want to close, use the "Option + Command + Left arrow". you can very quickly close a lot of array without ever clicking

Plonk answered 24/7, 2023 at 0:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.