Question

In Visual Studio you can minimize huge chunks of code using regions; they essentially just surround the code and minimize it in the window.

Does Sublime have a feature similar to this?

Was it helpful?

Solution

By default, you can select some code the go to Edit > Code Folding > Fold. There are tons of plugins that leverage the code-folding api for more options.

OTHER TIPS

There's a request on the official site to "ask for features" here.

But apparently:

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.

Jon being the programmer behind Sublime Text 2.

There might be a way to fake it by creating a plugin that looks for markers and removes the code region in between the markers, but it probably wouldn't look good. With the current API, it's probably your best bet!

By the way, there is some code folding in Sublime Text, if you hover your mouse next to the line number, you will see some arrows appearing when you can fold / unfold.

I ended up using custom comment tags, indented one level less than the code I want to fold. It doesn't look best, though it serves its purpose.

class Foobar {
    // ...some code

// <fold
    function foo() {
    }
    function bar() {
    }
// </fold

    // more code...
}

This (at the moment) folds to:

class Foobar {
    // ...some code

// <fold[...]
// </fold

    // more code...
}

Having a native ST2 support for this would be nice.

This looks what you are looking for. You can define tags for #region and #endregion for each language, or a generic tag for all of them.

If you are obsessed with intendation, this solution may make you uncomfortable but here it is, once upon a time while I had been writing a semi-complex jQuery plugin I've had constants, variables, private and public functions sections and foldings like so;

;(function($, undefined, window) {...

/* Consts */

    var FOO = "BAR";

/* Variables */

    var m_Foo = "bar";

/* Functions */

    /* Public Functions */

        function foo() {...}

        function bar() {...}

    /* Private Functions */

        function _foo() {...}

        function _bar() {...}

})(jQuery, window);

As you can see it is all about intendation. Sections can be folded; Consts, Variables, Functions. And also inside Functions section, Public Functions and Private Functions are both can be folded.

You can also use one line comment (//) to name your fold. So the idea underneath that is simple; ST2 thinks that the more intended lines belongs to first less-intended comment above them, like C/C++ compilers how handle brackets as own unique code blocks.

To fold the code select the code and press

ctrl + shift + [

To unfold the code put the cursor there and press

ctrl + shift + ]

I think that like myself, the OP has come to appreciate a little-known feature in VS called regions that many equate to code-folding, but is FAR more powerful and above, Dio Phung provided the answer that I wanted, and I suspect the OP wanted, but he didn't share as an answer so here it is.

The difference between "code-folding" as it's provided in Sublime Text is that it's based on code/compiler syntax while "regions" and what this plugin does, allow you infinitely more freedom, though it's a freedom that's more or less dependant on the code you're working with to begin with (deeply nested, or properly modularized).

If you are on Sublime Text 3, here is a plugin that can do it : github.com/jamalsenouci/sublimetext-syntaxfold – Dio Phung

In languages which support 3 types of comments (e.g. PHP) I use the hashtag comment for regions, as shown in the other answers. It's also good for keeping track of what's being done

# default options
    $a = 3;
    $b = 'bob';


$old_code = 1;

# bugfix #130
    $result = magic_function($data);
    fix_stuff($result);

$old_code = $result;

Otherwise use triple slash ///, or //# etc.

In sublime text, it works like this, it shades the lines you want to collapse and presses (Control + Shift +?)

I have the most recent version of sublimetext.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top