SublimeText3 コンフィグメモ
概要
markdown でよく使う`
(バッククオート)を入力した時に"
や'
を入力した時にやってくれるような自動ペアリング処理をさせたい。
経緯的な何か
そもそも以前の ST3 では"
や'
や()
,{}
など、デフォルトではいい感じに動いていなかったが現在の SublimeText3(build 3114)では
のあたりの議論がいつの間にか公式に取り込まれた模様。
つまり、ペアにする文字を追加で設定するするのではなくキーバインディングで`
が入力されたときの動作設定をごそっと追加する方式。
参考にする設定
なので現在の ST3 ではデフォルトのキーバインディング設定が参考になる。
メニュー > Preferences > Key Bindings > Default
を開いて// Auto-pair single quotes
とコメントされている辺りのコンフィグを見つける。
他にも// Auto-pair xxxxx quotes
とか// Auto-pair xxxx brackets
という感じで設定されているので自分で見やすい奴をピックアップ。
見つけたコンフィグのクオート文字を`
に変更したものを User のキーバインド設定に追加すれば良い。
コンフィグする
Preferences > Key Bindings > User に設定を追加する
メニューから以下を開いて設定を追加する
Preferences > Key Bindings > User
Default
に書き込むとST3のアップデートとかで書き換わる可能性がある(っていうか多分書き換えられたのでこのメモを作ることになった)ので User
のキーバインドに設定する。
実物はこんな感じ。
[
// Auto-pair back quotes
{ "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": "preceding_text", "operator": "not_regex_contains", "operand": "[`a-zA-Z0-9_]$", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "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 }
]
},
{ "keys": ["`"], "command": "move", "args": {"by": "characters", "forward": true}, "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": "^`", "match_all": true },
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true },
]
},
{ "keys": ["backspace"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Delete Left Right.sublime-macro"}, "context":
[
{ "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
{ "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 },
{ "key": "selector", "operator": "not_equal", "operand": "punctuation.definition.string.begin", "match_all": true },
{ "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.single - punctuation.definition.string.end", "match_all": true },
]
},
]