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.
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 }
]
}