27

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?

piperchester
  • 1,176
  • 2
  • 14
  • 25
  • 3
    Just a clarification, that's a Visual Studio feature, not C#. It seems there is some "code folding"feature but it does not seem to work as in VS. – madth3 Dec 13 '12 at 22:55
  • 1
    Just a side note: [StyleCop](http://stylecop.codeplex.com/) rejects the [use of regions](http://www.stylecop.com/docs/SA1124.html). – Filburt Dec 13 '12 at 23:06
  • 1
    If you are on Sublime Text 3, here is a plugin that can do it : https://github.com/jamalsenouci/sublimetext-syntaxfold – Dio Phung May 13 '16 at 18:15

9 Answers9

22

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.

BoundinCode
  • 13,183
  • 5
  • 37
  • 29
  • 9
    I will assume you're the kind person who downvoted me without providing a comment. Can you please provide a solution to the given problem, which is `regions` like in Visual Studio? Please point to a plugin that does it, thank you. – emartel Dec 14 '12 at 02:15
  • 1
    The downvote was for the misinformation. You cited a very old source about code folding not being implemented. Jon has since changed his mind and added full support for code folding in ST2 (API here: http://www.sublimetext.com/docs/2/api_reference.html). Could you clarify what you mean by `regions` in VS? I'm not too familiar, especially because `regions` means something different in ST. – BoundinCode Dec 14 '12 at 02:45
  • 2
    Regions in Visual Studio are defined with `#region RegionName` / `#endregion` tags which collapse to a single line saying `RegionName`. I've used ST2 a lot and haven't seen a plugin that does that yet. – emartel Dec 14 '12 at 02:47
  • And using ST2 regions it would be possible to add these kind of regions, but on the `API` level there's no way to trigger manually the "folding arrows" to provide a good integration, so that's why I said it probably wouldn't look too good – emartel Dec 14 '12 at 02:49
  • Hmm that seems really simple to make with a plugin. Right now there are a few plugins that I know of that provide this functionality (plus they do a lot of over stuff) but they're keyboard-driven. Off the top of my head: BracketHighlighter 2 beta allows for folding of custom regions so you could add #region and #endregion. RegReplace also allows custom folding regions but it's a bit more complicated. It wouldn't be too much work to make a custom plugin with a mouse-map though. I'll see what I can come up with. – BoundinCode Dec 14 '12 at 03:02
  • If I wasn't already working on something else I would have whipped something up (it's been a while since I released a ST2 plugin!), I'll check the two plugins you mention but I like "simple" plugins that are "to the point", thanks for sharing – emartel Dec 14 '12 at 03:04
11

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.

emartel
  • 7,500
  • 1
  • 28
  • 56
11

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.

rdamborsky
  • 1,900
  • 1
  • 16
  • 21
  • Why does that work/what's that called? Does it work b/c it's considered a code block simply based on the indentation? Ugly, but it does work awfully well. Thanks! – ruffin Aug 08 '17 at 16:52
6

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.

Adrian Lopez
  • 2,081
  • 5
  • 26
  • 44
3

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.

ozanmuyes
  • 588
  • 12
  • 24
2

To fold the code select the code and press

ctrl + shift + [

To unfold the code put the cursor there and press

ctrl + shift + ]

Manfred Radlwimmer
  • 12,469
  • 13
  • 47
  • 56
Virendra Singh
  • 278
  • 3
  • 9
0

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

rainabba
  • 3,067
  • 27
  • 29
-1

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.

Lemures
  • 444
  • 3
  • 11
  • I use the same standard, but your answer doesn't address how to add that feature/behavior, just your convention. – rainabba Oct 04 '18 at 18:06
-2

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.

  • "huge chunks of code using regions" 'regions' as in: https://docs.microsoft.com/en-us/previous-versions/visualstudio/visual-studio-2012/b6xkz944(v=vs.110) – rainabba Oct 04 '18 at 18:04