Sublime Forum

Add a space between brackets

#1

Hi, I’d like to know how to transform :

echo('Sublimtext');

To :

echo ( 'Sublimtext' );

But, adding the space for brackets on ly when I type :

echo([space]

So, If I type

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 :

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 :

{ "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.

0 Likes

Spaces around maths and logic operators
#2

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]{ “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 }

]
}[/code]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]{ “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 }

]
}[/code]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:

{ "key": "selector", "operator": "equal", "operand": "source.ruby" }where “source.ruby” should limit it to a Ruby context only.

0 Likes