Enable code-folding by default
Asked Answered
P

7

76

Is there a way to tell XCode fold methods/functions by default when opening a file?

Pleasing answered 2/3, 2010 at 15:56 Comment(0)
D
79

Not sure if this is an OS change, or an Xcode change, but on Lion with Xcode 4, some of these commands cited by Griffo are performed with the Option modifier (the ⌥ key), not control. I've made some changes to reflect this:

Fold                       ⌥⌘←      option+command+left
Unfold                      ⌥⌘→      option+command+right
Unfold All                  ⌥U        option+U
Fold Methods/Functions      ⌥⌘↑      option+command+up
Unfold Methods/Functions    ⌥⌘↓      option+command+down

Sorry to necro a thread, but I thought this was worthwhile to revise.

Distort answered 27/7, 2011 at 23:7 Comment(2)
On OS X, Xcode 4.3, the fold all/unfold all changed to shift-option-command-left/rightTwentytwo
In Xcode 5.1 fold/unfold Methods/Functions by Shift-Command-Option-Left/Right tooDigastric
H
105

If you want to fold / unfold specific blocks of code, since Xcode 7, you have to enable that.

Go to Xcode > Preferences > Text Editing > and check 'Code folding ribbon'.

Xcode's Text Editing preferences

(No idea why they disabled this useful feature by default.)

Huneycutt answered 29/9, 2015 at 14:45 Comment(3)
That's awfully annoying. I just re-enabled it - thanks for the tip!Fennelflower
Cannot find it in xCode 9 (beta)Air
@OfirMalachi that is because is has not been implemented!Dah
D
79

Not sure if this is an OS change, or an Xcode change, but on Lion with Xcode 4, some of these commands cited by Griffo are performed with the Option modifier (the ⌥ key), not control. I've made some changes to reflect this:

Fold                       ⌥⌘←      option+command+left
Unfold                      ⌥⌘→      option+command+right
Unfold All                  ⌥U        option+U
Fold Methods/Functions      ⌥⌘↑      option+command+up
Unfold Methods/Functions    ⌥⌘↓      option+command+down

Sorry to necro a thread, but I thought this was worthwhile to revise.

Distort answered 27/7, 2011 at 23:7 Comment(2)
On OS X, Xcode 4.3, the fold all/unfold all changed to shift-option-command-left/rightTwentytwo
In Xcode 5.1 fold/unfold Methods/Functions by Shift-Command-Option-Left/Right tooDigastric
H
67

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 + left


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

Heyduck answered 8/9, 2017 at 12:59 Comment(4)
Your answer is good. But apple is bad to take away the code folding ribbon function.Stalinism
Thanks for the animation, that makes it easy to understand. I don't see the right-click option in XCode 9.2, but the menu option and keyboard shortcut work fine so that's not a big deal. One tip for selecting the block of code is to double-click the top brace and it'll select to its matching bottom brace.Scever
I really needed that screenshot. That 'ribbon' is really hard to findDulcy
Really nice thanks. Sadly though seems like Fold All and Unfold All are no more. Also although this is very useful, this and the other answers do not answer the question posted. My advice would be to delete this answer and instead leave the other answers where you have copy pasted, e.g. #40286497Vassaux
C
18

The correct answer is in some of the comments (not in any of the answers, I don't think...

incidentally, the shortcut keys for this are here:

Picture of shortcut keys in Xcode for collapsing and expanding code

Judging from past answers, this must have changed over time. This is from Xcode 6.1.1.

Convexoconvex answered 8/3, 2015 at 22:19 Comment(0)
C
11

For the sake of completeness,

Fold All        ⌘⌥⇧←    command+option+shift+left
Cottbus answered 13/1, 2014 at 17:21 Comment(1)
Undocumented keyboard shortcut, but works perfectly. This should be marked as the correct answer.Minimal
R
9

I don't think so, there are some handy keyboard shortcuts though:

Fold                        ⌃⌘←      control+command+left
Fold All                    ⌘⌥⇧←     command+option+shift+left
Unfold                      ⌃⌘→      control+command+right
Unfold All                  ⌃U       control+U
Fold Methods/Functions      ⌃⌘↑      control+command+up
Unfold Methods/Functions    ⌃⌘↓      control+command+down
Fold Comment Blocks         ⌃⇧⌘↑     control+shift+command+up
Unfold Comment Blocks       ⌃⇧⌘↓     control+shift+command+down
Focus Follows Selection     ⌃⌥⌘F     control+option+command+F
Representational answered 3/3, 2010 at 14:59 Comment(2)
Hmmm, the keyboard shortcuts aren't showing up in firefox on Windows. The characterset with the command key symbol obviously doesn't exist. I'll follow up with text alternatives.Representational
Changed in Xcode5, for example Fold all Methods/Functions is now command+shift+option+left. +1 though for helping me find this.Gandzha
O
4

I feel your pain. It is difficult to press cmd+option+shift+left every time I open an implementation file to collapse all the methods. I would be grateful to know of a way to set this by default. On the other hand, a probably obvious workaround may be to select the method from the method picker at the top of the window. I suppose this is how the Xcode developers intended us to find methods.. still cmd+option+shift+left is my preference.. it just hurts my hand :/

Offbeat answered 23/8, 2011 at 3:22 Comment(2)
Actually I just started settling with the picker, too :/ But that's mostly because Xcode also seems to become kinda buggy with long files and code folding :(Pleasing
Recently discovered that if you select the picker with the mouse you can start typing a method name and it filters the picker list by what you type.Offbeat

© 2022 - 2024 — McMap. All rights reserved.