Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set bracket indentation behavior in ST3

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

like image 397
OffCS Avatar asked Jan 04 '17 05:01

OffCS


People also ask

How do I indent text in Sublime Text?

That's quite simple in Sublime. Just Ctrl+Shift+P (or Command+Shift+P on MacOS) to open the tools pallet, type reindent , and pick Indentation: Reindent Lines . It should reindent all the file you are in, just remember to save before running the command, or it may not appear.

Do you need curly braces in java?

Java has a "feature" which allows you to omit curly brackets when writing if statements, for loops, or while loops containing only a single statement. You should never use this feature – always include curly brackets. The reason for this is because omitting curly brackets increases your chances of writing buggy code.

How do I change the code in Sublime Text?

Netbeans like Shortcut KeySelect all code that you intend to indent, then hit Ctrl + ] in Sublime text to indent. For macOS users, use command + ] to indent, and command + [ to un-indent.


1 Answers

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 }
    ]
},
like image 140
Keith Hall Avatar answered Oct 11 '22 20:10

Keith Hall