【问题标题】:How to automatically insert a space between parentheses in Sublime Text 3 after hitting space击中空格后如何在 Sublime Text 3 中的括号之间自动插入空格
【发布时间】:2019-11-25 03:47:01
【问题描述】:

当我输入( 并点击Space 时,我希望Sublime Text 自动添加一个空格并移动插入符号,因此最终结果是( ^ ),其中^ 是光标。

如何调整键绑定以实现这一点?

我已经能够使用

调整键绑定
{ "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 }
        ]
    }

但是在输入( 时,这将始终将其格式化为( ^ )。我宁愿在点击space 后将其格式化。

我可以在默认绑定中看到在输入 { 后点击 enter 的情况,这将运行一个宏,所以我想我可以将它用作模板,但我无法在我的文件系统上找到该宏看看它是如何工作的。

{ "keys": ["enter"], "command": "run_macro_file", "args": {"file": "res://Packages/Default/Add Line in Braces.sublime-macro"}, "context":
        [
            { "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 }
        ]
    },

【问题讨论】:

  • 请参阅“How to Ask”以及链接页面和“mcve”。你研究过这个吗?在哪里?如果它没有帮助告诉我们原因。你尝试了什么?如果你不尝试,为什么不呢?如果你做了,你做了什么?我们希望看到您尝试解决这个问题。

标签: javascript sublimetext3


【解决方案1】:

您的初始绑定在这里有正确的想法,但是您想要将其绑定到按空格而不是按左括号;这将需要您首先手动打开成对的括号,然后按空格插入您想要的 sn-p:

    { "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": "preceding_text", "operator": "regex_contains", "operand": "\\($", "match_all": true },
        { "key": "following_text", "operator": "regex_contains", "operand": "^\\)", "match_all": true }
       ]
   },

现在按空格键将插入两个空格,光标在它们之间,但仅当光标直接位于两个括号之间时。

但是请注意,此绑定将停止默认存在的 Backspace 上的绑定,以便在您退格时删除两个括号,因为该绑定要求光标像这个一样被括号包围,但是一旦此绑定触发,情况将不再如此。

所以你也可以包含这个绑定,它会检测光标何时处于这种情况并运行与默认绑定相同的宏;这将允许您按 Space 插入空格,然后决定您不想要它们并按 Backspace 将它们删除。

    { "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 }
        ]
    },

我可以在默认绑定中看到输入 { 后按回车键的情况,这将运行一个宏,所以我想我可以将它用作模板,但我无法在我的文件系统上找到该宏来查看它是如何工作的.

Sublime 附带的软件包存储在sublime-package 文件中(扩展名已更改的 zip 文件)。从 core sublime 查看它们内部的最简单方法是使用命令面板中的View Package File,它会显示当前加载的每个包中的每个文件。然后,您可以输入过滤文本以缩小您想要查看的文件的范围,然后选择它来打开文件。

请注意,以这种方式打开的文件是只读的(因为它们来自包文件),以提醒编辑它们无效。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-26
    • 2017-07-22
    相关资源
    最近更新 更多