Home Download Buy Blog Forum Support

Auto completing brackets within quotes.

Auto completing brackets within quotes.

Postby h2shin on Fri Aug 03, 2012 3:12 pm

Hey guys.

At the moment when I open a parenthesis in newly opened quote marks, the parenthesis doesn't close automatically, is there a way to enable this behaviour in sublime text 2?

So instead of showing "(" it goes straight to "()"

Cheers..
h2shin
 
Posts: 4
Joined: Sun Jul 08, 2012 7:50 pm

Re: Auto completing brackets within quotes.

Postby nick. on Fri Aug 03, 2012 4:56 pm

Have a look at the following snippet in Preferences > Key Bindings -- Default:
Code: Select all
// Auto-pair brackets
   { "keys": ["("], "command": "insert_snippet", "args": {"contents": "($0)"}, "context":
      [
         { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
         { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
         { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true }
      ]
   },


You can change the operand of "regex_contains" to include a quote.
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Auto completing brackets within quotes.

Postby h2shin on Fri Aug 03, 2012 6:34 pm

nick. wrote:Have a look at the following snippet in Preferences > Key Bindings -- Default:
Code: Select all
// Auto-pair brackets
   { "keys": ["("], "command": "insert_snippet", "args": {"contents": "($0)"}, "context":
      [
         { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
         { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
         { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|$)", "match_all": true }
      ]
   },


You can change the operand of "regex_contains" to include a quote.



ahh yes... I'm not great with regular expressions, could you give me an idea as to how I could edit it?
h2shin
 
Posts: 4
Joined: Sun Jul 08, 2012 7:50 pm

Re: Auto completing brackets within quotes.

Postby nick. on Fri Aug 03, 2012 8:49 pm

It is a bit trickier since we're in JSON; you have to escape the quote (because the whole string is in double quotes), and then you have to escape the escape. Thus:
Code: Select all
"regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|\\\"|$)"


Full key binding:
Code: Select all
// Auto-pair brackets
   { "keys": ["("], "command": "insert_snippet", "args": {"contents": "($0)"}, "context":
      [
         { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
         { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
         { "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|]|;|\\}|\\\"|$)", "match_all": true }
      ]
   },


You may want to put that in your Key Bindings -- User so you don't lose the setting on update.
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Auto completing brackets within quotes.

Postby h2shin on Sat Aug 04, 2012 9:02 am

This is awesome. Thanks so much :)
h2shin
 
Posts: 4
Joined: Sun Jul 08, 2012 7:50 pm


Return to Technical Support

Who is online

Users browsing this forum: No registered users and 23 guests