1

这是现在发生的事情的一个例子:

.class {

}

所以当我输入.class { SublimeText时会自动插入}(这是正确的)。然后我按Enter并得到这个:

.class {
    #CURSOR_POSITION#
}

但我真正想要的是(注意右括号):

.class {
    #CURSOR_POSITION#
    }

我什至在一些编辑器的特殊设置中看到过(好吧,只有一次)。现在我开始使用 SublimeText(这很酷!),我觉得可以以这种方式自定义它,但我不太确定如何。

4

1 回答 1

2

好的!做到这一点并不难,但知道如何做到这一点并不简单 =)

所以我的做法是:

  1. 编写一个简单的宏(我已经编辑了Enter名为Add Line in Braces.sublime-macrowhich的默认宏~Data/Packages/Default)并用新名称保存它。我已经调用它CSS.Add Line in Braces.sublime-macro并放入了~Data/Packages/User.

    [
        {"command": "insert", "args": {"characters": "\n\n"} },
        {"command": "move_to", "args": {"to": "hardbol", "extend": false} },
        {"command": "insert", "args": {"characters": "\t"} },
        {"command": "move", "args": {"by": "lines", "forward": false} },
        {"command": "move_to", "args": {"to": "hardeol", "extend": false} },
        {"command": "reindent", "args": {"single_line": true} }
    ]
    
  2. 将其应用于EnterCSS 文件中的键。为此,我们需要转到 Preferences > Key Bindings - User (它将打开~Data/Packages/User/Default (YOUR_OPERATING_SYSTEM).sublime-keymap)并粘贴以下代码:

    [
        { "keys": ["enter"], "command": "run_macro_file", "args": {"file": "Packages/User/CSS.Add Line in Braces.sublime-macro"}, "context":
            [
                { "key": "selector", "operator": "equal", "operand": "source.css" },
                { "key": "setting.auto_indent", "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.css" } 它告诉 Sublime 仅将其应用于 CSS ext。

  3. 利润!

于 2012-07-09T00:29:12.500 回答