2

Is there a way to improve Sublime text 2's curly bracket wrapping? For example, I have the following PHP code:

$isThisPointless = true;
$uselessString = 'Meh.';
$uselessInt = 83;

Let's say I want to wrap it inside an if statement. So I go ahead, select the code and press '{' and Sublime Text magically wraps it in curly brackets. Great! Only, indentation is a mess. now my code is more like:

{$isThisPointless = true;
        $uselessString = 'Meh.';
        $uselessInt = 83;}

While I was expecting to achieve this instead:

{
    $isThisPointless = true;
    $uselessString = 'Meh.';
    $uselessInt = 83;
}

Google search suggested the bracketeer plugin. I tried using it and while it removes the ugly indentation, it still doesn't indent the block the way I want.

Is there a way to make Sublime Text do this?

Valerio Bulla
  • 366
  • 2
  • 11

2 Answers2

8

I've figured this out using a custom snippet:

<snippet>
    <content><![CDATA[
{
    $SELECTION$1
}
]]></content>
    <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
    <!-- <tabTrigger>hello</tabTrigger> -->
    <!-- Optional: Set a scope to limit where the snippet will trigger -->
    <scope>source.js, source.php</scope>
</snippet>

... and then binding that snippet to a key combination. If you use the snippet you might want to change the scope (depending on the language you need), or just disable it entirely.

I'll leave the answer here just in case somebody needs it!

Valerio Bulla
  • 366
  • 2
  • 11
  • 2
    http://stackoverflow.com/a/15582512/99256 this is how to add the shortcut for a snippet – MartyIX Jan 25 '14 at 19:36
  • 1
    Note that when copy and pasting the code in this answer, it'll have spaces on the line with `$SELECTION$1`, but Sublime Text recommends you always use tabs. That way, it'll properly be converted to the correct format of spaces or tabs in the output. – Tyler Collier Apr 27 '14 at 00:17
2

The snippet mentioned above didn't work for me, so I've created a plugin specifically for this purpose:

https://github.com/Epskampie/sublime_indent_and_braces

Simon Epskamp
  • 8,813
  • 3
  • 53
  • 58