Sublime Forum

Insert Curly Bracket Automatically

#1

Hey folks,

I am trying to set-up ST2 to insert curly brackets end of line, enter a line break, insert closing curly bracket and then move cursor up a line and then enter automatically, when I press “{”.

Basically “{” -> “{
[tab] |
}”
where “|” represents cursor. I could enter the two curly brackets by the key binding and the .macro:

{ "command": "move_to", "args": {"to": "eol"} }, { "command": "insert_snippet", "args": {"contents": "${TM_LINE_TERMINATOR:{}\n\n}"} } ]

But I do not know how to move the cursor up one line and then insert a tab. Could anyone give a suggestion? Much appreciated.

0 Likes

#2

use $0, $1, etc. for cursor positions. :smile:

Also, you can do ${0:SOME_RANDOM_TEXT} to give a tabstop with some highlighted text…

0 Likes

#3

By the way, this already happens if you hit enter after the brace. It’s a macro named ‘Add Line in Braces’ . I’ve added a few keybindings to have the behavior occur for ( and as well:

[code]/* Add lines on enter for {, (, */
{ “keys”: “enter”], “command”: “run_macro_file”, “args”: {“file”: “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”: “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 }
]

}[/code]

0 Likes

#4

Yes, you are right. Thanks for the heads-up. I will take a look at the bindings you provided. I think they will be useful. Is there a site where the popular key bindings are listed? That would be helpful…

0 Likes