Enable code-folding by default

ぐ巨炮叔叔 提交于 2019-11-29 21:15:38

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.

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'.

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

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

Look at this snapshot:


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:


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:

Same options from Xcode Short-cut list:

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

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:

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

For the sake of completeness,

Fold All        ⌘⌥⇧←    command+option+shift+left
conorgriffin

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

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 :/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!