在 Sublime Text 中自定义“换行”字符

Dev*_*ons 5 ruby sublime-text-2 sublime-text-3 sublime-text

在 Sublime Text 中,突出显示任何文本并按下以下任何符号:

'
"
(
{
[
Run Code Online (Sandbox Code Playgroud)

将导致整个突出显示的部分被您按下的键(或其匹配符号)包围。是否可以选择向此列表添加更多字符?我会经常|在 Ruby 开发中使用和`。

Mat*_*DMo 7

碰巧的是,我已经为反引号 (`) 设置了键绑定,因此为管道修改它们|应该很容易。为此,请打开Preferences -> Key Bindings-User. 如果文件为空,请[]在单独的行上添加左方括号和右方括号,并在它们之间粘贴以下内容:

// Auto-pair backticks
{ "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.double", "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 }
    ]
},
{ "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 }
    ]
},
Run Code Online (Sandbox Code Playgroud)

对于管道字符,您需要稍微修改一些正则表达式,因为|它具有特殊含义。以下对我有用:

// Auto-pair pipes in Ruby
{ "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.double", "match_all": true },
        { "key": "selector", "operator": "equal", "operand": "source.ruby", "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 },
        { "key": "selector", "operator": "equal", "operand": "source.ruby", "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": "equal", "operand": "source.ruby", "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": "equal", "operand": "source.ruby", "match_all": true }
    ]
}
Run Code Online (Sandbox Code Playgroud)

您会注意到我将选择器添加source.ruby|键绑定中 - 如果您想在除 Ruby 代码之外的其他文件中使用它,您可以删除或更改它。我没有将它添加到反引号代码中,因为它们可以在其他语法中使用——Python 和 Markdown 是我首先想到的。