Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.
share|improve this question
add comment

1 Answer

up vote 3 down vote accepted

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" }
  ]
},
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.