Meh 5 years ago • updated by Anew Level 1 month ago 82 1 duplicate
It's been on the roadmap for a while, would really contribute to the usable screen real-estate of the editor.

Duplicates 1

Especially if it the code folding was customizable from the filetype plugins like in vim. Ie, manual folding by default, javascript's folding setup based on brackets etc. Then it would be easily customizable for users too.
Loving that idea! If the .tmtheme file or whichever it would fall under, could control where new folds start and end, then Sublime would be the god of editors.
There is already a definition of the folding markers in the .tmLanguage files

<?xml version="1.0" encoding="UTF-8"?>

<plist version="1.0">

Also vim-compatible marker folds so that stuff between {{{ and }}} becomes a fold.

Jon, when you implement this please also give us some API methods.
Please! It could make for some really interesting plugins.
Wow, yes ... API would be great for this!

Yeah it would be cool if someone could make a Resharper type thing for Sublime Text

I feel like this is always a "cool" feature, but I don't know that I've ever once used it to do anything useful. I'm sure it'd be a lot of work to do, too.

The only time I think I'd use this is when editing HTML.
I find it can be useful in languages that encourage a lot of functions/methods per file (collapse all to definitions). Similarly it can be helpful in code with a lot of comments, like Python code that incorporates a lot of restructuredText comments (collapse comments), or languages that implement regions (e.g. C#'s #region).

It would also be useful to base other features on. For instance, in the Find In Files results buffer, multiple-line matches in each file could be collapsed to a single line per file.

Another example is when doing side-by-side file diff: you could collapse (hide) multiple-line sections which have no differences to display. This behavior can be seen in e.g. Beyond Compare: http://i.imgur.com/3JxOM.png and http://i.imgur.com/8m7Yj.png (a very nice diff tool btw).
Depends what you mean by 'useful', I suppose. This is a big deal for some folks, and is definitely the one thing stopping me from purchasing. Would make tmBundle compatibility more complete as well (start folding marker)

Main use for me is usable editor real-estate. (which Sublime is big on, so I'm hoping this feature will be a priority) Being able to fold up sections of code you're done with, or if you're reviewing code / large config files you can fold up sections you're done analyzing, etc. (e.g. If I need to track variables in functions 4 and 9, I can fold up 5-8 and see the relevant parts right next to each other)
This is the only feature stopping me from making the jump from Notepad++ / TextWrangler to a unified editor, but currently editing large xml files is a real pain without being able to fold sections.
As like a 188th person to up this, yeah. Bracket collapsing would be freaking awesome to have in sublime.
It's like one of very few, but very important reason that makes me say, "Shit. Notepad++ does top Sublime in this aspect."
I hate cote folding. I like goto-anything instead.
Don't use it then. Who asked for your preference?
What about XML files?
You're right for xml i use code folding. but for for C# i don't like the region thing because it's distracting.
I love code folding, Notepad++ does this why not Sublime Text 2 which cost so much for a license I am just expecting more of sublime text 2 to worth my money spend on and my time to use it.
Also I really want to have support of CodeIgniter (php framework)!
That's not related to code folding. If you find that important, start a separate topic for it.
Drop the code igniter text mate bundle into sublime text packages and hit it off.
FYI, Jon has stated that this is not possible in the current implementation of the editor control. Looks like we're waiting till V3 guys.
A shame, basically means I can't use Sublime Text 2. :/
If you don't want to, yes. But if you really do, like me, then clever use of marks makes it work out quite well.

Bookmarks has been the only thing saving me without code folding :\

Yeah I happened to see that the other day too. Such a shame, and he said V3 could be a good 2 years off too.
I honestly think this is something that Jon should put above all other fixes, drop everything else,  and work on.
I don't know how much deep in the engine he has to change to get this done, and I'm sure he has his reasons.
But Just look at the number of people who wants/needs this feature.
All the other fixes he's been doing lately are nice, but they also are things I can live without.
From the look of what Jon's saying (about the editor difficulties) this would require pretty much a complete rewrite of the editor (presumably showing/hiding areas has no chance of working with the current implementation, at least with keeping the excellent Python API for plugins working in any nice way), hence the V3 thing.

However, I do agree that it's a little worrying that this feature is not in a paid-for application (not that I'm currently paying for it for full disclosure, but I do plan to, not even sure what I'm waiting for any more) but luckily it's not a dealbreaker for me at all, would just be really nice. Especially if it works like Notepad++'s folding levels, that was seriously useful for getting a look at functions in a big class.
Well, I DID pay for sublime2, and Jon's gonna have to put up with some whiners like me,
because sublime not having code folding really feels like awesome Lamborghini not having AC installed.
Even a primitive and simple implementation of code folding will be extremely helpful
for people like me who has to work with code with thousands of lines.
(I didn't write this shit. Don't blame me.)

You should have not paid for it if it's a deal breaker for you.

Also edit that shit in something else and use Sublime for everything else.

Btw, I don't think you would complain if u could get a Lamborghini so cheap, just without aircon...


Cheap? I don't know about you, but I don't usually pay much for a text editor. $60 is most I ever spent on a text editor.

Pay for? Notepad++ and Scribes and what's that one java people like... Eclipse are all free.

That being said... I still think sublime is worth paying for. When it goes off beta I'll be buying it.

For long files of code try Ctrl+R (Goto Symbol) 

Here is a code folding plugin that might hold you over until Jon gets to it:

Code folding is one of those things that I don't use very often, but when I need it I really need it, usually in a complex XML file (I want to fold blocks, not hide selections of text.)

Without it Sublime Text 2 could replace TextMate for my Python editing (perhaps 70% of what I do) but unless it can completely (or at least 95%) replace TextMate I'm less interested in putting in the effort to make the switch. From my quick look so far this and no keyboard shortcut for "Find in project" are blockers for me.

I believe that cmd+shift+f is find in project. Also, new dev builds have code folding: http://www.sublimetext.com/dev 

It landed in today's build.
I've created a short tutorial and video on how to use Code Folding in Sublime 2  http://wesbos.com/sublime-text-code-folding/
Key-based code folding is definitely cool, but I hope the visual arrows are a planned feature for a future build as well!
Sidebar folding arrows are already in the latest development release.
Code folding is lost when a file is closed and re-opened. Quite useless if you work on the same files regularly. It would be VERY useful if Sublime can remember all of the code folding permanently. Perhaps store info about it in a settings file, or in a new directory with files called myfile.folded etc.
Cannot say for other systems, but on OS X metadata is the way to go.
Just don't clutter the project folders themselves with random editor files. I can't stand that. They like to sneak into source code control.
That's an extra step I shouldn't have to do. 
Plus then the gitignore gets committed, still cluttering up the git history.
The gitignore gets commited?

You know there is a global gitignore? 

That's the one you should be using to configure git on the machine you use with the apps you use.. you only need to do it once.

Heh, there's a global one? I just added the .gitignore inside the .gitignore file xD worked for me..
It's usually on your home folder..
Ah, good to know. :)
However, take care in using the global ignore file.

Ignoring files generated by 3rd party tools/editors not required by the project, fine.

Ignoring project-related files such as binaries and temp files because you don't want to maintain multiple project ignore files, no. Please don't do this. These files can sneak into the project this way by someone else cloning your project and being careless. It really is good to know about, but please don't abuse it =)

Good explanation, though this has gone considerably of topic, heh.. Good to know anyway, thanks!
One thing I like about using C#, is the region tagging so I didn't have to fold every single function, I could wrap a series of functions into a fold or region.  If the code folding had api hooks, I would love to setup something in my C++ code where I had taggable comments that would indicate the beginning and the end of foldable regions.  It would be awesome if I could do this, please make it happen! :)
Would it be possible to add an option to have all elements folded initially when the file is open. I currently do this in eclipse. It's very useful. I get a quick glance at all methods, properties, etc. Then I can expand specifically what I want to work on.

At the moment, the first thing I do when I open any file in sublime is invoke the fold all command manually. It would be cool, if I could configure it to do it automatically.
You can do that with a plugin, like this:

import sublime, sublime_plugin

class ExamplePlugin(sublime_plugin.EventListener):
    def on_load(self, view):

Isn't this feature complete?
Yeah, as far as I'm concerned it is! Should be closed.
I also think so.. there are quite a few topics still open which imo could be marked as resolved
It's very much not complete; notepad++ still has much better code folding. It's the major thing keeping me from switching entirely.
Great improvements with the last build !

still waiting for syntax/bracket-based code folding.

maybe i'm person nr 5000 asking for this, but it is the only thing keeping me from switching instantly to sublime text. 

i am using notepad++ atm (all day and every day) and the key features to me are side-by-side compare and code folding.

on Mac OS X i use sublime at the moment.

with proper indentation of my own code, (beware of someone elses) it is mostly usable.

one thing is really annoying with the existing folding mechanism:

on unfolding a certain area, everything inside is unfolded, even if parts of it were folded before.

would be a great step forward if these folds inside would remain folded as before.

if you have a class with 20 functions inside (most of them folded before), fold the class, unfold it later and you open a text-block with a few hundred lines of code inside, your'e lost :-(

Have you tried to use a tool like kaleidoscope? I can't understand how someone would prefer to make a side by side comparison in sublime than using a tool made for that. I'm sure it would only take a few minutes to make a plugin to interact with kaleidoscope, since is sports a command line interface.
If you want to edit different parts of the same file in different panes, you already can do that with the "Goto Anything..." command.

I agree on the unfolding behavior. There should exist a memory of what was or not folded previously and maintain those foldings.

Yes and no 
i am well aware of those myriad of diff/merge tools out there (ranging from brilliant to crap) and already using those fore that purpose.

i know this is a matter of personal preference and i'm sorry if my words were misleading, i rather meant side-by-side editing, which means having the template-file in one pane and its production-copy  in the other, where i am actively editing. 
having them both side by side helps a lot to keep track of the changes to be done and hitting "compare" from time to time reveals instantly if i edited the right parts or not.

P.S. and yes, i read the thread on that specific topic ;-)
For multiple-line folded sections I think it would be very helpful to have a horizontal line drawn across the folded section, possibly also annotated with the # of lines hidden. This screenshot gives a basic example of how this might look. In ST2's case you would probably want the horizontal line to be simpler looking and not insert any additional vertical space between the preceding and following lines.

A clickable [+] button in the left gutter of such folded sections would also be very handy.

Yes, an option for horizontal lines to be placed instead of the current ellipsis would be really appreciated. This is visually so much better to view at a glance than a small block with three dots in it. On the other hand these are really handy when the folded region does not cover only whole lines and really should not be disabled completely.

I believe that's what SciTE (and notpad++; Scintilla-based) do(es).

With the current version the code folding is tripped up by unindented #ifdef and similar preprocessor directives (which I always keep unindented).

I have started to change my code by indenting them (so folding works correctly) but this should be addressed.
Same for multi-line strings in Python (docstring style) that need to be printed on screen and thus are dedented w.r.t. the codeblock they belong to.
Undo/Redo of parts of code inside of a folded area do not cause the area to get unfolded unless that area is scrolled off-screen.

Has potential for causing confusion.
Another thing which is still missing is support for nested folds.
When I unfold a region that itself contains folded text, I would like to keep that text folded.
Example: Unfolding a class that contains folded methods, or in LaTeX: Unfolding a section that contains folded subsections.

I've created a new topic for this as it is not directly related: http://sublimetext.userecho.com/topic/116459-code-folding-support-for-nested-folds/

I'm using this feature in Notepad++ , there are key bindings to fold all / unfold all and it's VERY helpful for me.

Isn't this implemented now?


There is code folding but it does not work syntax-wise but indent-wise. This works well for languages that require you to indent your code, like Python or YAML, but might be problematic if you don't properly indent your C or Java code.

Interesting.  I'm slowly exploring this editor, there is a huge amount of hidden functionality.  I knew that I could tell it to auto collapse, but I didn't realize that it wasn't aware of the language structure.


Yes it is, but it's kinda bad compared to notepad++ folding. For example if some text is not too close to the left side it collapses folding of it's parent element. Same with comments inside code.


Not reading is clearly overrated: The #region directive drives me bonkers.

Please, don't forget that in C++, we use "#pragma region" and "#pragma endregion" too !
Same thing, I use XML folding but it would be good if folding would generate '+' icons that could be used for folding/unfolding.
I'm stuck between BBedit and Sublime Text 2... I would like to make a full jump. BBEdit intuitively knows at least HTML and XML for code folding. I think this is a minimum requirement for any modern editor. it doesn't matter whether it is indented or not. we get the proper arrows to collapse nodes. I use this a lot to see if there is a bad closing. You can have a <div id="big-elephant"> starting at the top and  it should, for example, close with two child nodes inside, but then if you fold it and see that it picked up ten child nodes... you know something is wrong...but there will be no error flag because the structure is still complete... just not what I want.  Without quick intuitive folding we are stuck... I have to paste back into BBEdit, test and then move it back to my Sublime Text 2 project. 
I've written a quick plugin that folds based on start and end markers for code regions, it might be useful for anyone programming in C++, C#, VB or using preprocessor directives. Take a look: https://sublime.wbond.net/packages/SyntaxFold
Not sure if this is the correct place for this, but it seems to be inline with the thread...

I'd like to have the option for my project/files to remember which sections were collapsed (and those that were expanded) when I save and/or quit Sublime. It would save screen real estate so that I can focus on the parts that I'm currently working on.