10

Sublime Text has 3 ways to handle brackets indentation when I hit a new line button.

1.curly bracket

xxx = {
  |cursor|
}

2.parenthesis

xxx = (
  |cursor|)

3.square bracket

xxx = [
|cursor|]

How could I set all of them to behave like curly bracket

MattDMo
  • 100,794
  • 21
  • 241
  • 231
OffCS
  • 441
  • 3
  • 15

1 Answers1

12

In the Default keybindings, there is this:

{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
    [
        { "key": "setting.auto_indent", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\{$", "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\}", "match_all": true }
    ]
},

which provides the functionality for pressing Enter between the { and } braces. The macro adds 2 newlines, moves the cursor to after the first one and reindents the line.

You can therefore achieve the same functionality between ( and ) and [ and ] by adding this to your user keybindings:

{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
    [
        { "key": "setting.auto_indent", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
    ]
},
{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
    [
        { "key": "setting.auto_indent", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "preceding_text", "operator": "regex_contains", "operand": "\\[$", "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\]", "match_all": true }
    ]
},
Keith Hall
  • 15,362
  • 3
  • 53
  • 71
  • 2
    Hi. This helped me somewhat, but I still get different behaviour. With parantheses the closing one will be indented with the cursor and for square brackets the cursor isn't indented at all.. – ViggoV Dec 27 '17 at 12:54
  • I guess this is due to the indentation rules ST is using in your case @ViggoV - which language/syntax are you experiencing this behavior with? – Keith Hall Dec 27 '17 at 13:03
  • Javascript/ES6. I found this: https://stackoverflow.com/questions/22865347/smart-indenting-of-brackets-parenthesis-in-sublime-text-2, but it didn't do the trick.. I've reverted to vscode for the time being... – ViggoV Dec 27 '17 at 14:00
  • I had the "the closing paren will be indented with the cursor" problem as well. This answer plus https://stackoverflow.com/questions/22865347/smart-indenting-of-brackets-parenthesis-in-sublime-text-2#comment39926321_23672859 fixed it for me in all languages :) But yeah, square brackets are broken differently and I haven't found a fix for those. – Bad Request Feb 12 '18 at 19:48