Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom bracket auto-closing in Sublime Text

I'm trying to auto-close the asterisk (*) character in Markdown files.
I've been looking through all the language setting files, and am turning up nothing to use as an example. I've also tried writing a snippet, but found it inefficient (it doesn't wrap around the selection).

I searched around and found BracketHighlighter (which claims to allow custom auto-close pairings) but with no luck (installed through Package Control, also restarted).

Any ideas on where I should start or what I'm doing wrong?


Solution (thanks to @skuroda)

skuroda's answer will do fine - however, I've made a few tweaks that I would like to append to their answer:

{ "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": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }

    ]
}

Which adds two ** if the asterisk key is pressed next to two preceding asterisks (e.g. **| then ***| becomes **|** where | is the cursor. This helps a lot with emboldening text.

like image 323
Albert Xing Avatar asked Apr 15 '13 22:04

Albert Xing


2 Answers

You may need to tweak the context some, but this should be a start. This is based on the auto pair key bindings for the built in brackets.

{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*$0*"}, "context":
    [
        { "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": "text.html.markdown", "match_all": true }

    ]
},
{ "keys": ["*"], "command": "insert_snippet", "args": {"contents": "*${0:$SELECTION}*"}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true },
        { "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
    ]
},
{ "keys": ["*"], "command": "move", "args": {"by": "characters", "forward": true}, "context":
    [
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\*", "match_all": true },
        { "key": "selector", "operator": "equal", "operand": "text.html.markdown", "match_all": true }
    ]
}
like image 109
skuroda Avatar answered Sep 27 '22 22:09

skuroda


Use this

{ "keys": ["*"], "command": "insert_snippet", "args": {"name": "Packages/User/my-snippet.sublime-snippet" }}

now go to Preference>Browse Packages and then User folder Create a file

my-snippet.sublime-snippet

and use following code inside

<snippet><content><![CDATA[
*${0:$SELECTION}*
]]></content></snippet>

Good Luck

like image 39
Kaiser Avatar answered Sep 27 '22 21:09

Kaiser