There are a couple of solutions. The first is to use the folding keyboard shortcut (CtrlShift[ on Windows/Linux, ⌘Shift[ on Mac) with the cursor either before or after the colon :
at the very end of the function signature - outside the parentheses ()
. You can unfold using CtrlShift] (⌘Shift]), or by using the arrow in the gutter just to the left of the def
keyword.
The second method is a little more involved, but allows you to use the fold arrows as well as the keyboard shortcuts, and your cursor can be anywhere when you fold. Open the Command Palette with CtrlShiftP (⌘ShiftP on Mac) and type v
, then select the View Package File
option. Next, type in python/fold
and select Python/Fold.tmPreferences
. A PLIST/XML file will open up, containing language-specific rules for folding.
Before we can edit the file, we need to save it and turn off its read-only protection. First, save the file by selecting File → Save As…
. It should automagically select the Packages/Python
directory, where Packages
is
- Linux:
~/.config/sublime-text-3/Packages
or ~/.config/sublime-text/Packages
- macOS/OS X:
~/Library/Application Support/Sublime Text 3/Packages
or ~/Library/Application Support/Sublime Text/Packages
- Windows Regular Install:
C:\Users\YourUserName\AppData\Roaming\Sublime Text 3\Packages
or C:\Users\YourUserName\AppData\Roaming\Sublime Text\Packages
- Windows Portable Install:
InstallationFolder\Sublime Text 3\Data\Packages
or InstallationFolder\Sublime Text\Data\Packages
The exact path depends on whether or not you upgraded from Sublime Text 3.
Next, select View → Show Console
and type in
view.set_read_only(False)
You can close the Console by hitting Esc or selecting View → Hide Console
.
In Fold.tmPreferences
, select lines 24-29 and comment them out - Ctrl/ for Win/Lin, ⌘/ for macOS. It should look something like this:
<string>punctuation.section.arguments.begin</string>
<key>end</key>
<string>punctuation.section.arguments.end</string>
</dict>
<!-- <dict>
<key>begin</key>
<string>punctuation.section.parameters.begin</string>
<key>end</key>
<string>punctuation.section.parameters.end</string>
</dict>
--> <dict>
<key>begin</key>
<string>punctuation.section.sequence.begin</string>
<key>end</key>
lines 20-33
Finally, save your changes. Test that everything worked correctly by closing the file after you save it, then going to File → Open Recent
and verifying that Fold.tmPreferences
is at the top of the list. Click on it to open it, and double-check that lines 24-29 are still commented out. Once you've done that, you can close it again.
And that should be it! Once the changes to Fold.tmPreferences
have been saved, they'll be effective immediately.
A big thank you to the regulars on the Sublime Text Discord server for their suggestions to look at Fold.tmPreferences
.