Home Download Buy Blog Forum Support

Add a space between brackets

Add a space between brackets

Postby Const on Wed Jun 19, 2013 9:48 pm

Hi, I'd like to know how to transform :
Code: Select all
echo('Sublimtext');

To :
Code: Select all
echo ( 'Sublimtext' );


But, adding the space for brackets on ly when I type :
Code: Select all
echo([space]


So, If I type
Code: Select all
echo(

Sublimetext will add the close ) and it's good, but then, if I add a space between, then, Sublimtext add two spaces, placing the cursor in the middle :
Code: Select all
echo ( | )

(the pipe is the cursor) and if it could add a space before the parenthese it would be great, too.

I tried this, but it's not working :
Code: Select all
{ "keys": ["space"], "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": "preceding_text", "operator": "equal", "operand": "(", "match_all": true }
   ]
}


But I want to do it only if there is a space after the (

Thank you for your attention.
Const
 
Posts: 3
Joined: Sun May 12, 2013 8:46 pm

Re: Add a space between brackets

Postby Inkling on Mon Dec 09, 2013 6:46 am

You need to use a literal space character for the key for some reason: [" "] rather than ["space"]

My solution - add this to your user keybinds file:
Code: Select all
{ "keys": [" "], "command": "insert_snippet", "args": {"contents": " $0 "}, "context":
  [
    { "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 }
  ]
}
Same thing can be done for other kinds of brackets if you wish (e.g. curly or square) by changing "(" and ")" in the operand strings.

Note if you wanna add another space before the opening bracket at the same time, you can record a macro (I don't know if there's a simpler way), the sequence would probably be:
  • left arrow
  • insert a space
  • right arrow
  • insert 2 spaces
  • left arrow
Then for the keybind:
Code: Select all
{ "keys": [" "], "command": "run_macro_file", "args": {"file": "res://Packages/User/Macro name goes here.sublime-macro"}, "context":
  [
    { "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 }
  ]
}
Edit the name/path of the macro as required. If using ST2 you might have to omit the "res://" bit.

If you wanna limit either of the above solutions to a certain language, you can add a scope selector line in the context array, for example:
Code: Select all
{ "key": "selector", "operator": "equal", "operand": "source.ruby" }
where "source.ruby" should limit it to a Ruby context only.
Inkling
 
Posts: 3
Joined: Mon Dec 09, 2013 6:24 am


Return to Technical Support

Who is online

Users browsing this forum: Google [Bot], valerij_ and 20 guests

cron