46

Can't find one under

Xcode ▶ Preferences/Text Editing ▶ "Code Folding Ribbon" box.

Krunal
  • 77,632
  • 48
  • 245
  • 261
Yakiv Kovalskyi
  • 1,737
  • 1
  • 15
  • 29
  • Beta version bugs should be reported to [Apple Bug Reporter](https://bugreport.apple.com) instead. – Raptor Jun 07 '17 at 01:23
  • 4
    Well, it's 2018 now and XCode folding is worse than ever. We all want to fold if-else but Apple says we don't. – Dog Feb 08 '18 at 14:37
  • @Yakiv Kovalsky - Code folding ribbon is back in Xcode 10 but different way – Krunal Jun 11 '18 at 09:27

6 Answers6

94

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


Xcode 9

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

ROOT
  • 11,363
  • 5
  • 30
  • 45
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • 2
    This code folding is missing from preferences of Xcode 9, inside 'text editing' tab. I want that back :( – user1039695 Aug 29 '17 at 17:59
  • 2
    In Xcode 9 Beta 6, the key to press seems to be the Command key (not control). Folding of blocks within a method doesn't seem to work with Objective-C yet, although with Swift it works most of the time. – user2067021 Aug 30 '17 at 00:57
  • 12
    This doesn't work for long if and for statements. That's where it's more important. – strangetimes Sep 13 '17 at 09:16
  • 1
    @Insider I agree with you dear, but how can I even say, why they have removed this very useful feature/option? I just have alternate/work around solution to it. – Krunal Oct 01 '17 at 13:38
  • 4
    It is the missing ribbon that is making things extremely difficult. As in, I am looking for a new editor tonight. I really can't believe that it is gone. I realize now that I use it *constantly*. – SAHM Oct 04 '17 at 00:42
  • Why does Apple not give complete pleasure (improve one thing disturb existing things)? – ViruMax Jan 22 '18 at 10:15
  • Is there a way to fold just the variables? For example, suppose you have a section with a bunch of outlets. Can you fold just the outlets? – Victor Engel Jul 31 '20 at 17:02
10

Concurring with comment by @strangetimes, I submitted to Apple Bug Reporter the following, in the new SUGGESTION format. Please feel free to pile on to Apple with your own:

35237858 : Code Folding in Xcode 9.1 Only does Entire Functions/Methods

UPDATE, A YEAR LATER…

This is fixed in Xcode 10. Code folding is back. Thanks to all those who piled on to the bug report or whatever.

The new, improved code folding in Xcode 9 or 9.1 seems to only work on an entire function or method. (I'm working in Objective-C today.) This is not very useful. You know, functions or methods are only one curly bracket deep. It is easy to eyeball them. Where I need code folding is for long control flow blocks – if, else, switch, do, while, or just loose blocks. Please bring it back so I can use code folding as it worked in Xcode 8.

The ribbon thing, although I thought it was good, others didn't – I can live without it. But code folding needs to work on all curly-bracket code blocks please.

Jerry Krinock
  • 4,860
  • 33
  • 39
  • Thanks for sharing. I've just created a Suggestion as well. **36023358: Allow folding just the block of code the cursor is at in Xcode 9.2** – Daniel Dec 13 '17 at 18:00
  • In Xcode 10, folding and unfolding works on blocks of code, not only on entire methods. It's time to really get used to Command-Option-Left Arrow and Command-Option-Right Arrow. This is what most of us have been waiting for. – Dog Dec 29 '18 at 04:00
2

I have the same question and I found this at forums.developer.apple.com

...isn't implemented in the new editor yet. @https://forums.developer.apple.com/thread/78912

stone
  • 21
  • 3
2

This doesn't do folding, but it might help you to navigate in the code:

If you want to find out which brackets that go together you can click just behind the start or end bracket, you will then get a one second highlight of both. Or you can double click or either bracket and get permanent highlight of everything between the brackets.

Sten
  • 3,624
  • 1
  • 27
  • 26
1

It is fixed now in Xcode_9_beta_5

Alaeddine
  • 6,104
  • 3
  • 28
  • 45
  • 7
    The code-folding ribbon is still missing. This makes it extremely difficult when working with long functions. – SAHM Oct 04 '17 at 00:43
0

Xcode 9

As it has already been commented, the code folding in Xcode 9 only works on entire functions. The closest I got to a way to figure out where does a curly brace closes (especially for long for or if blocks), is by double tapping in the curly brace. This will highlight that block of code until the curly brace closes.

Of course, this will work much better in Xcode 10 with the new code folding ribbon.

Jonathan Cabrera
  • 1,656
  • 1
  • 19
  • 20