Home Download Buy Blog Forum Support

Insert Curly Bracket Automatically

Insert Curly Bracket Automatically

Postby arin on Thu Feb 23, 2012 7:16 pm

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:

Code: Select all
[
    { "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.
arin
 
Posts: 23
Joined: Thu Feb 23, 2012 7:10 pm

Re: Insert Curly Bracket Automatically

Postby C0D312 on Thu Feb 23, 2012 8:12 pm

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

Also, you can do ${0:SOME_RANDOM_TEXT} to give a tabstop with some highlighted text...
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Insert Curly Bracket Automatically

Postby nick. on Thu Feb 23, 2012 8:38 pm

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: Select all
/* 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 }
   ]
}
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Insert Curly Bracket Automatically

Postby arin on Thu Feb 23, 2012 9:00 pm

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..
arin
 
Posts: 23
Joined: Thu Feb 23, 2012 7:10 pm


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 22 guests