Automatically insert a space between parentheses in Sublime Text 2
Is there some configuration or plugin I can use which would add spaces to the inside of parentheses when they're matched. Here's an example of what I'm trying to explain.
if (^) // ^ represents cursor position
if ( ^ ) // Where I want the cursor to be positioned.
Solution 1:
You can edit the auto-pair functionality. I copied the following from "Key Bindings - Default" into "Key Bindings - User". Add spaces in the contents
values. You can do something similar for square and curly brackets. The first setting sets it for normal use. The second sets it for when you have text highlighted.
// 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 }
]
},
{ "keys": ["("], "command": "insert_snippet", "args": {"contents": "( ${0:$SELECTION} )"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]
},
Edit
To make the key binding syntax specific, add a line at the bottom of the context
values. You will have to find the syntax's scopeName
. For example html is text.html
and sass is source.sass
.
{ "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 },
{ "key": "selector", "operator": "equal", "operand": "source.sass" }
]
},