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

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

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

'
"
(
{
[

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

答案1

碰巧的是,我已经设置了反引号 (`) 的键绑定,因此修改它们以适应竖线|应该很容易。为此,请打开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 }
    ]
},

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

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

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

相关内容